Get run time for two programs simultaneously Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Get run time for two programs simultaneously without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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


@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: %sn" $(cat "${realtime1}")
printf "Number of seconds used by program2: %sn" $(cat "${realtime2}")

The test:

$ bash
Number of seconds used by program1: 5.00
Number of seconds used by program2: 10.00
We are here to answer your question about Get run time for two programs simultaneously - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji