P is for Practical | |
PerlMonks |
Re^2: Confirming what we already knewby Aristotle (Chancellor) |
on Mar 09, 2003 at 05:51 UTC ( [id://241505]=note: print w/replies, xml ) | Need Help?? |
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: and the call becomes
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.
In Section
Meditations
|
|