user775171
user775171

Reputation:

Test speed of two scripts

I'd like to test the speed of a bash script and a Python script. How would I get the time it took to run them?

Upvotes: 1

Views: 434

Answers (3)

Daniel Pereira
Daniel Pereira

Reputation: 1785

At the beginning of each script output the start time and at the end of each script output the end time. Subtract the times and compare. Or use the time command if it is available as others have answered.

Upvotes: 0

Piskvor left the building
Piskvor left the building

Reputation: 92752

If you're on Linux (or another UN*X), try time:

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard error giving timing statistics about this program run. These statis- tics consist of (i) the elapsed real time between invocation and termi- nation, (ii) the user CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2)), and (iii) the system CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2)).

Note that you need to eliminate outer effects - e.g. other processes using the same resources can skew the measurement.

Upvotes: 3

Mihai Maruseac
Mihai Maruseac

Reputation: 21435

I guess that you can use

time ./script.sh
time python script.py

Upvotes: 0

Related Questions