slitaz-doc-wiki-data diff pages/en/devnotes/ash-benchmarks.txt @ rev 7

Add pages/en folder.
author Christopher Rogers <slaxemulator@gmail.com>
date Sat Feb 26 12:17:18 2011 +0000 (2011-02-26)
parents
children df9950c99d41
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pages/en/devnotes/ash-benchmarks.txt	Sat Feb 26 12:17:18 2011 +0000
     1.3 @@ -0,0 +1,174 @@
     1.4 +Some tips to speed-up scripts... Add yours, and put the better ones on the top of page :)
     1.5 +
     1.6 +==== Sed substitution vs Variable substitution ====
     1.7 +
     1.8 +**Information**
     1.9 +
    1.10 +In some case, both tools can do the same job. As a build-in ash/bash command, variable substitution is a faster.
    1.11 +Note: this is always true - you can compare each type of variable substitution with an equivalent using an external tool.
    1.12 +
    1.13 +**Benchmark**
    1.14 +<code>
    1.15 +$ echo '#!/bin/sh
    1.16 +for i in $(seq 1 1000); do
    1.17 +	echo "slitaz" | sed 's/slitaz/SliTaz/'
    1.18 +done' > /tmp/slow
    1.19 +$ echo '#!/bin/sh
    1.20 +for i in $(seq 1 1000); do
    1.21 +	A=slitaz
    1.22 +	echo "${A/slitaz/SliTaz}"
    1.23 +done' > /tmp/speed
    1.24 +$ chmod +x /tmp/slow /tmp/speed
    1.25 +$ time /tmp/slow
    1.26 +> real	0m 12.40s
    1.27 +$ time /tmp/speed
    1.28 +> real	0m 0.04s
    1.29 +</code>
    1.30 +
    1.31 +==== Group command vs. Sub-process ====
    1.32 +
    1.33 +**Information**
    1.34 +
    1.35 +Group command "{}" group several commands into one (as a function). So, output can be grouped too: "{ com1; com2; } | com3". Sub-process "()" achieve something similar, but create a shell sub-process; which cost a lot more resources. Another difference is that, when you kill an application using CTRL^C, sub-process is killed instead of main application - while CTRL^C on grouped commands kill the application itself. Finally, changing directory or variables into sub-process will not affect main script while it does with grouped commands. Conclusion: always use group command instead of sub-processes, and take care ;D
    1.36 +
    1.37 +//Note:// group command need a newline before closing - or "; }".
    1.38 +
    1.39 +**Benchmark**
    1.40 +<code>
    1.41 +$ echo '#!/bin/sh
    1.42 +for i in $(seq 1 10000); do
    1.43 +	( echo yo )
    1.44 +done' > /tmp/slow
    1.45 +$ echo '#!/bin/sh
    1.46 +for i in $(seq 1 10000); do
    1.47 +	{ echo yo; }
    1.48 +done' > /tmp/speed
    1.49 +$ chmod +x /tmp/slow /tmp/speed
    1.50 +$ time /tmp/slow
    1.51 +> real	0m 5.36s
    1.52 +$ time /tmp/speed
    1.53 +> real	0m 0.23s
    1.54 +</code>
    1.55 +
    1.56 +==== Grep vs Fgrep ====
    1.57 +
    1.58 +**Information**
    1.59 +
    1.60 +fgrep is exactly the same thing that grep if you don't use patterns (^,$,*,etc.). Fgrep is optimized to handle such case, particularly when you look for several different plain patterns. A difference can be found even if you look of only one pattern.
    1.61 +
    1.62 +**Benchmark**
    1.63 +<code>
    1.64 +$ echo -e "line1\nline2\nline3" > /tmp/file
    1.65 +$ echo '#!/bin/sh
    1.66 +for i in $(seq 1 1000); do
    1.67 +	grep 3 /tmp/file
    1.68 +done' > /tmp/slow
    1.69 +$ echo '#!/bin/sh
    1.70 +for i in $(seq 1 1000); do
    1.71 +	fgrep 3 /tmp/file
    1.72 +done' > /tmp/speed
    1.73 +$ chmod +x /tmp/slow /tmp/speed
    1.74 +$ time /tmp/slow
    1.75 +> real	0m 11.87s
    1.76 +$ time /tmp/speed
    1.77 +> real	0m 3.21s
    1.78 +</code>
    1.79 +
    1.80 +==== [ -n "text" ] vs [ "text" ] ====
    1.81 +
    1.82 +**Information**
    1.83 +
    1.84 +The two commands test if "text" exists. Using -n slow the process a little and weight the script a little too.
    1.85 +
    1.86 +**Benchmark**
    1.87 +<code>
    1.88 +$ echo '#!/bin/sh
    1.89 +for i in $(seq 1 1000000); do
    1.90 +	[  -n "$i" ]
    1.91 +done' > /tmp/slow
    1.92 +$ echo '#!/bin/sh
    1.93 +for i in $(seq 1 1000000); do
    1.94 +	[ "$i" ]
    1.95 +done' > /tmp/speed
    1.96 +$ chmod +x /tmp/slow /tmp/speed
    1.97 +$ time /tmp/slow
    1.98 +> real	0m 15.56s
    1.99 +$ time /tmp/speed
   1.100 +> real	0m 14.11s
   1.101 +</code>
   1.102 +
   1.103 +==== [ -z "text" ] vs [ ! "text" ] vs ! [ "text" ] ====
   1.104 +
   1.105 +**Information**
   1.106 +
   1.107 +Theses three commands test if text doesn't exist. [ ! "text" ] and [ -z "text" ] have a similar processing time, while ! [ "text" ] is speeder.
   1.108 +
   1.109 +**Benchmark**
   1.110 +<code>
   1.111 +$ echo '#!/bin/sh
   1.112 +for i in $(seq 1 1000000); do
   1.113 +	[  -n "$i" ]
   1.114 +done' > /tmp/slow1
   1.115 +$ echo '#!/bin/sh
   1.116 +for i in $(seq 1 1000000); do
   1.117 +	[  -n "$i" ]
   1.118 +done' > /tmp/slow2
   1.119 +$ echo '#!/bin/sh
   1.120 +for i in $(seq 1 1000000); do
   1.121 +	[ "$i" ]
   1.122 +done' > /tmp/speed
   1.123 +$ chmod +x /tmp/slow1 /tmp/slow2 /tmp/speed
   1.124 +$ time /tmp/slow1
   1.125 +> real	0m 15.53s
   1.126 +$ time /tmp/slow2
   1.127 +> real	0m 15.60s
   1.128 +$ time /tmp/speed
   1.129 +> real	0m 14.27s
   1.130 +</code>
   1.131 +
   1.132 +==== Awk vs Cut ====
   1.133 +
   1.134 +**Information**
   1.135 +
   1.136 +Awk, as cut, can be used to cut a field of a line. Awk can do many other things, while cut is a tool dedicated to this usage; it's why cut is a little faster for this task.
   1.137 + 
   1.138 +**Benchmark**
   1.139 +<code>
   1.140 +$ echo -e "field1\tfield2\tfield3" > /tmp/file
   1.141 +$ echo '#!/bin/sh
   1.142 +for i in $(seq 1 5000); do
   1.143 +	awk '"'"'{ print $2 }'"'"' /tmp/file
   1.144 +done' > /tmp/slow
   1.145 +$ echo '#!/bin/sh
   1.146 +for i in $(seq 5000); do
   1.147 +	cut -f2 /tmp/file
   1.148 +done' > /tmp/speed
   1.149 +$ chmod +x /tmp/slow /tmp/speed
   1.150 +$ time /tmp/slow
   1.151 +> real	0m 16.61s
   1.152 +$ time /tmp/speed
   1.153 +> real	0m 15.90s
   1.154 +</code>
   1.155 +
   1.156 +==== [ condition1 -a condition2 ] vs [ condition1 ] && [ condition2 ] ====
   1.157 +
   1.158 +**Information**
   1.159 +
   1.160 +While && is a fast built-in function, in this case it uses two process (two test functions) instead one. So, using -a is a little faster, as the "AND" function itself is slower but make possible to use only one process.
   1.161 +
   1.162 +**Benchmark**
   1.163 +<code>
   1.164 +$ echo '#!/bin/sh
   1.165 +for i in $(seq 1 1000000); do
   1.166 +	[ "$i" ] && [ "$i" ]
   1.167 +done' > /tmp/slow
   1.168 +$ echo '#!/bin/sh
   1.169 +for i in $(seq 1 1000000); do
   1.170 +	[  "$i"  -a "$i" ]
   1.171 +done' > /tmp/speed
   1.172 +$ chmod +x /tmp/slow /tmp/speed
   1.173 +$ time /tmp/slow
   1.174 +> real	0m 23.94s
   1.175 +$ time /tmp/speed
   1.176 +> real	0m 22.29s
   1.177 +</code>
   1.178 \ No newline at end of file