A beginning programmer only cares about making the program work. Given sufficient time, an experienced programmer cares about some other factors: performance, security, future scalability and maintainability... I don't claim to be very experienced but happened to have time to do benchmarking on some language constructs in Perl, my favorite language, thanks to an understanding boss at Shell. I didn't use the module Benchmark.pm specifically designed for benchmarking. But my results should be useful to some extent. All tests were run on my SPARCStation-20 running Solaris2.5.1 with Perl5.004 unless otherwise stated. Each script was run about 5 times and the time it took was averaged.
-------------------------------------------------------------------------------- #!.../perl5.004 -w $start=(times)[0]; $tmp="1234.56789"; for ($i=0; $i<100000; $i++) { $temp=int $tmp; #1.81 seconds Best #$temp=substr $tmp,0,(index $tmp,"\."); #2.37 sec Second Best #$temp=sprintf "%d",$tmp; #3.18 sec Poor #$temp=(split /\./,$tmp)[0]; #7.42 sec Worst } print $temp; $end=(times)[0]; printf "\nThis script takes %.2f CPU seconds.\n", $end-$start; -------------------------------------------------------------------------------- system "ls > /dev/null"; #about the same fast as the next line exec "ls > /dev/null"; #on one directory I tested (ca. 13 sec for 50 cycles) -------------------------------------------------------------------------------- if (/a/) #slower than the next line if ($_ eq "abcd") -------------------------------------------------------------------------------- for ($i=0; $i<2000; $i++) { open TMP, "myfile"; while () { if (!/no rows selected/) #0.47 sec #chomp; if ($_ ne "no rows selected") #0.43 sec, about the same fast { print "OK\n"; } } close TMP; } -------------------------------------------------------------------------------- $tmp="b"; for ($i=0; $i<100000; $i++) { if ($tmp=~/[abc]/) #2.07 sec, just to confirm what Jeffrey Friedl #says, "character class is very fast" #if ($tmp=~/(a|b|c)/) #2.57 sec #if ($tmp=~/(?:a|b|c)/) #2.14 sec #if ($tmp=~/(?=a|b|c)/) #2.23 sec #if ($tmp=~/(?!a|b|c)/) #2.73 sec {#code snipped } } -------------------------------------------------------------------------------- $a="a"; $b="b"; for ($i=0; $i<1000000; $i++) { if ($a eq $b) {} #2.00 sec on WindowsNT runnign ActivePerl5.005_03 #if ($a ne $b) {} #2.09 sec } --------------------------------------------------------------------------------
To my Computer Page