The run times of scripts vary quite much. I would like to find similar time -function as Matlab's timeit, described here.
For instance, I am evaluating these commands here about counting quickly matches, running time LC_ALL=C grep -ao CDA r328_0002.raw | wc -l
in loop
---------------------------------------------
Events real user sys
----------- ----------- ----------- ---------
40 0m0.044s 0m0.042s 0m0.005s
40 0m0.064s 0m0.062s 0m0.005s
40 0m0.046s 0m0.044s 0m0.005s
40 0m0.043s 0m0.042s 0m0.005s
40 0m0.047s 0m0.044s 0m0.005s
---------------------------------------------
Table: Events when Macbook Air 2013-Mid in Power Supply.
---------------------------------------------
Events real user sys
----------- ----------- ----------- ---------
40 0m0.056s 0m0.041s 0m0.011s
40 0m0.060s 0m0.047s 0m0.008s
40 0m0.041s 0m0.039s 0m0.006s
40 0m0.046s 0m0.044s 0m0.006s
40 0m0.047s 0m0.045s 0m0.006s
---------------------------------------------
Table: Events when Macbook Air in Battery Supply, 6h later.
where you see the real-time fluctuates from 0.044s to 0.064s, the user-time from 0.042s to 0.062s, while the sys-time keeps rather stable at 0.005s. My idea in timing
- iterate the command first 1k before timing
- do timing 10 times and take the average and standard deviation
Stout to /dev/nul
This idea is in lcd047's comment, running time LC_ALL=C ggrep -ao CDA r328_0002.raw >/dev/null
in loop
--------------------------------------------
real user sys
-------------- -------------- ------------
0m0.006s 0m0.003s 0m0.002s
0m0.006s 0m0.003s 0m0.002s
0m0.006s 0m0.003s 0m0.002s
0m0.008s 0m0.003s 0m0.003s
0m0.006s 0m0.003s 0m0.002s
0m0.005s 0m0.002s 0m0.002s
0m0.006s 0m0.002s 0m0.002s
0m0.009s 0m0.003s 0m0.003s
0m0.007s 0m0.003s 0m0.003s
0m0.006s 0m0.003s 0m0.002s
0m0.006s 0m0.003s 0m0.002s
0m0.008s 0m0.003s 0m0.003s
--------------------------------------------
Table: Events when Macbook Air 2013-Mid in Battery Supply.
I think these times can be even more improved when keeping the laptop in power supply and keeping less programs on.
How can you time stably shell scripts?
i=0; while [ "$((i+=1))" -lt 1000 ]; do cat r*0002.raw;done | time LC_ALL=C grep ...
and so on. – mikeserv Jul 01 '15 at 08:09time LC_ALL=C grep -ao CDA r328_0002.raw >/dev/null
. Console output is always slow, and is highly influenced by what's going on in the rest of the system. Also, if possible run your benchmarks at a time when the system is idle. If you want accuracy do something like 10 runs, drop the shortest and the longest time, and then average the results. And add enough data to make the tests last longer, preferably >10s, the longer the better. – lcd047 Jul 01 '15 at 11:59