Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: Confirming what we already knew

by genecutl (Beadle)
on Mar 06, 2003 at 00:18 UTC ( [id://240756]=note: print w/replies, xml ) Need Help??


in reply to Re: Confirming what we already knew
in thread Confirming what we already knew

While I don't know how much of the difference between the perl and c code this makes up, after a cursory scan I do see a number of inefficiencies in the perl code. The first thing that jumps out at me are all the my calls inside the loops. If you are going to be declaring these variables so many times, it's more efficient to do so outside the loops. Here's a quick benchmark:
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% --
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.,
for(my $i = 64; $i < (scalar(@rddt) - (2 * $pond)); $i++){
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:
@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% --
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.

Replies are listed 'Best First'.
Re: Re: Re: Confirming what we already knew
by AssFace (Pilgrim) on Mar 06, 2003 at 02:17 UTC
    *smacks forehead*
    I got sloppy with the "my"s for sure. Ugh. Normally I'm fairly careful about such things - but I think I slipped up here for sure.
    I went through and took out all of the "my" declarations and did put them outside of loops.

    The result of that benchmarked at 268 seconds - *but* this is on my laptop that normally does it in 305 seconds (Athlon M 1G on WinXP with Active State Perl and half a gig of RAM). So a good speed up so far.

    Then changing the calculation to be just before its for loop instead of up top.
    That brought ended up one second slower than the above code.
    So then moving it up above the highest loop made it go from 268 to to 286.
    Then just putting in a number there and having no calculation at all in there brings it down to 264 seconds.
    Doesn't seem like the improvement that I would expect to see after looking at your benchmark... but still an improvement - largely from that stupid "my" thing that I just screwed up on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://240756]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-18 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found