Unix command for benchmarking code executing K times

Suppose I have code running on Unix as follows:

$ ./mycode 

My question is whether there is a way in which I can run my code K times. For example, the value K = 1000.

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

+7
c ++ performance benchmarking unix
source share
6 answers

try

 $ time ( your commands ) 

write a loop to go to parens to repeat the command if necessary.

Update

Well, we can solve the command line task too long. This is the bash syntax, if you use a different 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 
+13
source share

improve / clarify Charlie's answer:

 time (for i in $(seq 10000); do ./mycode; done) 
+14
source share

Just a tip: make sure that this “test” is close to your actual use of the executed program. If this is a short life process, significant overhead may arise, caused only by the creation of the process. Do not assume that this is the same as implementing this loop in your program.

+5
source share

To improve some of the other answers a little, some of them (those based on seq) can lead to a too strong command line if you decide to test, say, a million times. The following restrictions do not have

 time ( a=0 ; while test $a -lt 10000 ; do echo $a ; a=`expr $a + 1` ; done) 
+3
source share

Another solution to the “too long command line” problem is to use the C loop loop in bash:

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

This also works in zsh (although I'm sure zsh has some more convenient way to use it, I'm just still new to zsh). I can’t test others because I have never used others.

+2
source share

If you are worried about the overhead of constantly loading and unloading the executable file into the process space, I suggest you configure the disk with the disk and the time from your application.

In the 70s, we used the opportunity to set a “sticky” bit in an executable file and leave it in memory. I don't know any unix that now supports this behavior, since it made updating apps a nightmare ....: o)

0
source share

All Articles