I am trying to make a shell script that could run tests on two differents programs simultaneously and compare their runtime in order to see which one has the best efficiency (tests have to be automated through this script)

Basically, I was wondering if there was a way to fork in a shell script and launch simultaneously my two programs to check their runtime and calculate the difference

  • 1
    I think you can use time program in your script: linux.die.net/man/1/time – coffeMug May 15 '16 at 19:53
  • 4
    Almost by definition, a program will run as fast as its bottleneck allows it to. Running both programs concurrently raises a risk that you're measuring how the kernel handles them competing for cpu/IO/... rather than their performance. – Ulrich Schwarz May 15 '16 at 20:02

@ulrichschwarz is right. The performance tests should not run both processes in parrallel.


You could use time which is a shell keyword.

On my debian box there is also a binary in the PATH which has more option. In that case, \time needs to be used to run the binary version.

There is no explicit fork. I mean the shell does everything for you when a program is started from the command line or from a script.

The shell starts a program in the background when & is added at the end of the command line: e.g. sleep 6 &

If 2 programs are started in the background, they are running simultaneously.

wait is a shell builtin. When used with no parameter, the current process is suspended to await the termination of the currently active child processes (in the background).

Give a try to this:

\time -o "${realtime1}" -f "%e" sleep 5 &
\time -o "${realtime2}" -f "%e" sleep 10 &
printf "Number of seconds used by program1: %s\n" $(cat "${realtime1}")
printf "Number of seconds used by program2: %s\n" $(cat "${realtime2}")

The test:

$ bash script.sh
Number of seconds used by program1: 5.00
Number of seconds used by program2: 10.00
  • super! mark this one as the answer. – Jay jargot May 15 '16 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.