in reply to Re: Confirming what we already knew
in thread Confirming what we already knew
So in this example, declaring the my variables outside of the loop gives a 15% speed up. Another problem I found was calculating the loop limiting condition in the for loop. e.g.,Benchmark::cmpthese(5000, { 'outside' => sub { my $x; my $y; my @z; my $i; for ($i = 0; $i < 1000; $i++) { $x = $i; $y = $x; @z = ($x, $y) } }, 'inside' => sub { for (my $i=0; $i < 1000; $i++) { my $x = $i; my $y = $x; my @z = ($x, $y)} } }); Rate inside outside inside 120/s -- -13% outside 138/s 15% --
Since it doesn't look like the size of @rddt or the value of $pond is changing, you should do that calculation outside of the loop. Here are the benchmarks:for(my $i = 64; $i < (scalar(@rddt) - (2 * $pond)); $i++){
A 65% speed up here. There are probably lots of other optimizations that you could do in this perl code. Those two were the most obvious.@rddt = (1) x 1000; $pond = 32; Benchmark::cmpthese(500, { 'inside' => sub { for ( my $i = 64 ; $i < ( scalar(@rddt) - ( 2 * $pond ) ) +; $i++ ) { $x++; } }, 'outside' => sub { $limit = scalar(@rddt) - ( 2 * $pond ); for ( my $i = 64 ; $i < $limit ; $i++ ) { $x++; } }, }); inside 273/s -- -39% outside 450/s 65% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Confirming what we already knew
by AssFace (Pilgrim) on Mar 06, 2003 at 02:17 UTC |