Get run time for two programs simultaneously

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

Answer

@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:

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

The test:

$ bash script.sh
Number of seconds used by program1: 5.00
Number of seconds used by program2: 10.00

Leave a Reply

Your email address will not be published. Required fields are marked *