Yep, chaining is faster, which makes sense. But figured I'd try to compare them anyway.<br><br>Here are two scripts that I used for comparison:<br><br><span style="font-weight: bold;">test1.pl</span>--------------------------------------------------------------<br>
<br>#!/usr/bin/perl -w<br><br>use strict;<br><br>sub loop<br>{<br> my ($a, $b, $c, $d) = "";<br> for my $i (0..300000)<br> {<br> $a = get(0);<br> $b = get(5);<br> $c = get(10);<br> $d = get(14);<br>
<br> $a = $b if $a eq "-";<br> $a = $c if $a eq "-"; <br> $a = $d if $a eq "-";<br> $a = "empty" if $a eq "-";<br><br> #print "$i - a: $a, b: $b, c: $c, d: $d", "\n";<br>
}<br>}<br><br>sub get<br>{ <br> my $r = 4;<br> my ($m) = @_;<br> my $a = int(rand($r)) + $m;<br> return $a if $a > ($m + $r) - 2;<br> return "-";<br>}<br>--------------------------------------------------------------<br>
<br><span style="font-weight: bold;">test2.pl</span>--------------------------------------------------------------<br>#!/usr/bin/perl -w<br><br>use strict;<br><br>sub loop<br>{<br> my ($a, $b, $c, $d) = "";<br>
for my $i (0..300000)<br> {<br> $a = get(0);<br> $b = get(5);<br> $c = get(10);<br> $d = get(14);<br><br> $a = $a || $b || $c || $d || 'empty';<br><br> # print "$i - a: $a, b: $b, c: $c, d: $d", "\n";<br>
}<br>}<br><br>sub get<br>{ <br> my $r = 4;<br> my ($m) = @_;<br> my $a = int(rand($r)) + $m;<br> return $a if $a > ($m + $r) - 2;<br> return 0;<br>}<br><br>loop();<br>--------------------------------------------------------------<br>
<br>Here are the results from the tests ran on the above scripts:<br><br><span style="font-weight: bold;">test1.pl</span><br>jbl@blopez-d620:~/tmp$ time ./test1.pl <br><br>real 0m2.129s<br>user 0m2.120s<br>sys 0m0.008s<br>
jbl@blopez-d620:~/tmp$ time ./test1.pl <br><br>real 0m2.023s<br>user 0m2.016s<br>sys 0m0.004s<br>jbl@blopez-d620:~/tmp$ time ./test1.pl <br><br>real 0m1.983s<br>user 0m1.976s<br>sys 0m0.008s<br><br><span style="font-weight: bold;">test2.pl</span><br>
jbl@blopez-d620:~/tmp$ time ./test2.pl <br><br>real 0m1.612s<br>user 0m1.604s<br>sys 0m0.008s<br>jbl@blopez-d620:~/tmp$ time ./test2.pl <br><br>real 0m1.579s<br>user 0m1.576s<br>sys 0m0.004s<br>jbl@blopez-d620:~/tmp$ time ./test2.pl <br>
<br>real 0m1.625s<br>user 0m1.612s<br>sys 0m0.012s<br><br><br clear="all"><br>-- <br>J. Bobby Lopez<br>Web: <a href="http://jbldata.com/">http://jbldata.com/</a><br>Twitter: <a href="http://www.twitter.com/jbobbylopez">http://www.twitter.com/jbobbylopez</a><br>