Differences between revisions 2 and 3
Revision 2 as of 2011-02-07 09:23:36
Size: 1468
Editor: Lhunath
Comment:
Revision 3 as of 2011-02-07 09:39:19
Size: 1367
Editor: Lhunath
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
As though you didn't already have enough reason to dump `expr`.
Line 22: Line 20:
|| 0m0.642s || {{{i=( {1..100000} ); time { echo "${i[@]}" >/dev/null; } }}} ||
Line 24: Line 23:

Another good reason to always quote your expansions.
Line 34: Line 31:

`[[` isn't just more featureful, it's also twice as fast.

Some rough benchmarks

To compare different approaches to a problem with regards to performance, here's some rough benchmarks which may be system-dependent. Feel free to run each test yourself to see how your system fares.

Results are always sorted with the winners on top. All tests were repeated multiple times to make sure deviations did not exceed acceptable levels or cross result boundaries.

Math

Time

Code

0m1.153s

j=0; time for i in {1..100000}; do ((j++)); done

0m1.381s

j=0; time for i in {1..100000}; do j=$((j+1)); done

0m1.498s

j=0; time for i in {1..100000}; do let j++; done

5m8.448s

j=0; time for i in {1..100000}; do j=$(expr $j + 1); done

Quoting

Time

Code

0m0.642s

i=( {1..100000} ); time { echo "${i[@]}" >/dev/null; } 

0m1.538s

printf -v i '%s ' {1..100000}; time { echo "$i" >/dev/null; } 

0m3.112s

printf -v i '%s ' {1..100000}; time { echo $i >/dev/null; } 

Testing

Time

Code

0m1.099s

time for i in {1..100000}; do [[ -d . ]]; done

0m2.110s

time for i in {1..100000}; do [ -d . ]; done

3m47.509s

time for i in {1..100000}; do /bin/test -d .; done

BashBenchmark (last edited 2012-05-22 06:01:31 by ormaaj)