Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have two scripts that each compute the factorial of a number. I would like to know which is faster. The time command gives me milliseconds and the result is different from time to time:

piousbox@piousbox-laptop:~/projects/trash$ time ruby fac2.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.089s
user    0m0.052s
sys 0m0.028s
piousbox@piousbox-laptop:~/projects/trash$ time ruby fac1.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.091s
user    0m0.048s
sys 0m0.036s
piousbox@piousbox-laptop:~/projects/trash$ time ruby fac1.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.088s
user    0m0.048s
sys 0m0.040s
piousbox@piousbox-laptop:~/projects/trash$ time ruby fac2.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.088s
user    0m0.048s
sys 0m0.028s
piousbox@piousbox-laptop:~/projects/trash$ time ruby fac1.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.087s
user    0m0.064s
sys 0m0.028s
piousbox@piousbox-laptop:~/projects/trash$ time ruby fac2.rb
30414093201713378043612608166064768844377641568960512000000000000

real    0m0.089s
user    0m0.068s
sys 0m0.016s
piousbox@piousbox-laptop:~/projects/trash$ 

How do I take the average time it take to run the script? I could parse and average out the output of a 100 time's but I imagine there is a better solution?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

No, your idea of averaging is correct.

Script execution depends on lots of factors, and however it is to be split up between setup time (loading interpreter in memory, setting up, and possibly compiling code to bytecode or machine code) and true execution time.

To better focus on inner execution time, you do the loop in the script itself (i.e. instead of calculating one factorial, you calculate it 100 times within one execution of the script. The script will be setup once, and the inner routine will execute 100 times).

To focus on total time, you execute the script one hundred times and average the results. Ideally, you should separate those executions enough that the system returns in a "reference state" (or a script-unrelated state) every time. For example, the interpreter itself will be cached in memory, so that the very first execution of the script will be appreciably slower than the subsequent ones.

share|improve this answer
    
Right, thanks. I noticed that the subsequent calls where getting shorter. I run the loop inside the scripts now, and found that one algorithm is definitely faster than the other. –  Victor Piousbox Jun 5 '13 at 7:29

You can run iterations of the program in a loop; and divide the total time by the number of iterations:

time for i in {1..10}; do sleep 1; done
real    0m10.052s
user    0m0.005s
sys 0m0.018s
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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