views:

383

answers:

6

Hi,

Suppose I have a code executed in Unix this way:

$ ./mycode

My question is is there a way I can time the running time of my code executed K times. The value of K = 1000 for example.

I am aware of Unix "time" command, but that only executed 1 instance.

+8  A: 

try

$ time ( your commands )

write a loop to go in the parens to repeat your command as needed.

Update

Okay, we can solve the command line too long issue. This is bash syntax, if you're using another shell you may have to use expr(1).

$ time (
> while ((n++ < 100)); do echo "n = $n"; done
> )

real    0m0.001s
user    0m0.000s
sys     0m0.000s
Charlie Martin
+10  A: 

to improve/clarify on Charlie's answer:

time (for i in $(seq 10000); do ./mycode; done)
Evan Teran
this may cause a command line too long.
Diego Sevilla
+4  A: 

Just a word of advice: Make sure this "benchmark" comes close to your real usage of the executed program. If this is a short living process, there could be a significant overhead caused by the process creation alone. Don't assume that it's the same as implementing this as a loop within your program.

paprika
That is absolutely right.
Diego Sevilla
+1. At the very least, please time a loop that runs a no-op program 1000 times just to get an idea of the overhead. This will still be an underestimate of the overhead because the real program will take longer for the dynamic linker to load and fix up.
j_random_hacker
+2  A: 

To enhance a little bit some other responses, some of them (those based on seq) may cause a command line too long if you decide to test, say one million times. The following does not have this limitation

time ( a=0 ; while test $a -lt 10000 ; do echo $a ; a=`expr $a + 1` ; done)
Diego Sevilla
A: 

If you're worried about the overhead of constantly load and unloading the executable into process space, I suggest you set up a ram disk and time your app from there.

Back in the 70's we used to be able to set a "sticky" bit on the executable and have it remain in memory.. I don't know of a single unix which now supports this behaviour as it made updating applications a nightmare.... :o)

ScaryAardvark
once run the program will be cached, so the repeated overhead will be relocation which won't be helped by using a ram disk
Ronny Vindenes
Mmm... I'm afraid that the RAM disk will only help a little bit. The SO still has to load the program and set up all the complex structures that support it.
Diego Sevilla
Caching the program is only half the battle. If you don't have pre-linking enabled it needs to be linked at runtime and it may incur significant startup overhead compared to runtime if it needs to initialise its environment or load from a config file.
Adam Hawes
+1 Diego. Process startup time seems fast but it's an eternity compared to the 1 or 2 machine instructions it takes to perform the loop *inside* a single instance of the application. That applies even if the OS doesn't look at the disk.
j_random_hacker
The sticky bit, incidentally, is no longer supported because modern OSes are smart enough to cache libraries and applications automatically, without needing any hints like that.
bdonlan
+1  A: 

Another solution to the "command line too long" problem is to use a C-style for loop within bash:

 $ for ((i=0;i<10;i++)); do echo $i; done

This works in zsh as well (though I bet zsh has some niftier way of using it, I'm just still new to zsh). I can't test others, as I've never used any other.

fow