Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Confirming what we already knew

by Aristotle (Chancellor)
on Mar 09, 2003 at 05:51 UTC ( [id://241505]=note: print w/replies, xml ) Need Help??


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

Tons of numerics and lots of array lookups - the kind of job that lends itself well to C. That said, I see quite a bit of room in your Perl.

There's loads of pointless temporary variables and intermediate assignments. Why do @params = @_? Just use @_ directly, there's nothing special about it. List::Util is also likely to hugely speed up parts of your job. Your if/elsif chains are not helping either. The ccond() function f.ex should be written along these lines, using aforementioned module:

my @ccond = sub { $rddt[$_[0]] - sum(@rrdt[$_[0]-12 .. $_[0]-1]) / 12; }, sub { $rddt[$_[0]] - sum(@rrdt[$_[0]-50 .. $_[0]-1) / 50; }, sub { $rddt[$_[0]] - min(@rddt[$_[0]-5 .. $_[0]-1]); }, # ... );
and the call becomes
$cv = sprintf "%0.2f", $ccond[$cond]->($i);

That way, rather than rippling through the entire if/elsif cascade every time, the correct code block is selected in constant time. An analogous change applies to the other function.

Obviously, this approach will be much harder to translate into C. As you can see, properly Perlish code would also have been drastically shorter than your offering.

Will those practices let Perl beat the C version? Not likely. However, I'm fairly confident that given a capable Perl programmer, resorting to C will only be required very rarely. (And note that the min and sum functions from List::Util I used here are written in C. So in a way, you have outsourced your C rewriting to CPAN authors - not a bad deal IMO.)

Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found