Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Confirming what we already knew

by perrin (Chancellor)
on Mar 05, 2003 at 23:47 UTC ( [id://240749]=note: print w/replies, xml ) Need Help??


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

This is the sort of code where I would expect to be faster in C. Lots of numerical comparisons, and not much else. However, there are a couple of things that jump out at me. One is your mvsrt() routine. I think you could remove that entirely by changing this line:
my $tv = mvsrt($cv, $oper, $amt);
to this:
my $tv = ( ($cv <=> $amt) == ($oper - 1) );
(Well, that changes the actual operation each value of $oper performs, but you get the idea.)

Also, you have lots of C-style for loops which could be rewritten to use foreach. For example, change this:

my $absMax = 0; for(my $i = $params[1] - 5; $i < $params[1]; $i++){ if($rddt[$i] > $absMax){ $absMax = $rddt[$i]; } } $returnValue = $rddt[$params[1]] - $absMax;
to this:
my $absMax = 0; # loop over array slice foreach my $rddt_value (@rddt[($params[1] - 5) .. $params[1]]) +{ if($rddt_value > $absMax){ $absMax = $rddt_value; } } $returnValue = $rddt[$params[1]] - $absMax;
Foreach loops do tend to run quite a bit faster, and you have many of these.

Replies are listed 'Best First'.
Re: Re: Re: Confirming what we already knew
by AssFace (Pilgrim) on Mar 06, 2003 at 03:54 UTC
    I read another suggestion and have taken out the "my" declarations within the loops, and I moved the limit declaration outside of the for loops.

    That brought me down from 305 to 264 seconds per ticker. (note that I am on a different machine that before - this is a laptop with an Athlon M 1G processor, half a gig of ram, WinXP, and Active State Perl.

    So using your suggestions to use my $tv = ( ($cv <=> $amt) == ($oper - 1) ); instead of the sub call.
    That, with the previous changes mentioned above, resulted in a new time of 289 seconds... so slower (and from what I can tell it corrupts the algorithm decision - so I'm going to scrap that one).

    So back to the way it was prior, and then replacing the for loops with foreach like you suggest gives me a new time of 237 seconds.

    So in the end I had a drop of nearly 70 seconds from what this code did prior to the optimizations. Over 2000 stocks that would save me over a day and a half of processing... but it is still not a huge difference (in comparison to what I saw in C that is).

    Had my mistakes being corrected brought the speed down to 20-30seconds per stock, I would have been very impressed - but for now, I still think I will use my method of coding it in Perl (perhaps sloppily) and then seeing from there what speed improvements are needed (if any) for it to be useful.

    UPDATE:
    Now that I'm back in on the P4 2G, I ran the updated code on that and it is now at 179 secs - previously at 196.
    I guess slight variations in speed changes come from the random loop variations each time it is run. Also I'm not sure what versions of ActiveState perl is on my laptop compared to here on this machine.
    For this code I've noticed that the Athlon tends to improve more easily than Intel - why that is, I don't know - perhaps cache sizes? No clue.
      That's funny, I just did a little benchmark and got a speedup of 6% by removing mvsrt. Not a huge difference though. Maybe my fake data isn't right.

      I'm not surprised that the C code is still much faster, and that is clearly the way to go with this, but it's cool to see a 22% speedup (305 secs down to 237) just from simple syntax-level changes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-16 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found