http://www.perlmonks.org?node_id=767044


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.