Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Improving performance of checksum calculation

by Crackers2 (Parson)
on May 30, 2009 at 16:15 UTC ( #767044=note: print w/ replies, xml ) Need Help??


in reply to Re: Improving performance of checksum calculation
in thread Improving performance of checksum calculation

* You should be using binmode on your input handle.

In the real program this is in a subroutine which gets pre-opened handles passed in; I just added a quick open at the top here to have a runnable snippet.

* ($check_value & 0x7fffffff) << 1 might be safer (more portable) than ($check_value << 1) & 0xffffffff

It more clearly shows what it's actually trying to do as well I think. Thanks.

* The following is faster (but nowhere near as fast as C would be):
$check_value ^= unpack('L', $_); $check_value = ( ($check_value & 0x7fffffff) << 1 ) | ( $check_value >> 31 );

As shown in my reply to BrowserUK below (which I'll post in a little bit), this cut my time from 1:38 to 1:26, or about a 12% improvement.

* Using C doesn't require shelling out. perlxstut or Inline::C will allow you to access C code from perl.

I looked at Inline::C, but that appears to require a compiler to be available at runtime, which isn't guaranteed. Still it might be worth doing a "use it if it's there" type thing.

So in the end I took BrowserUK's C code below and did all the magic xs invocations to get an .so; I'm including that inside an eval, so if the .so is not there it'll fall back to the slow perl implementation.


Comment on Re^2: Improving performance of checksum calculation
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2015-07-31 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls