Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://767044]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-04-21 06:29 GMT
Find Nodes?
    Voting Booth?