Hmm, well, it's going to be hard to get rid of 'bigint' if you want 64-bit arithmetic to work correctly :(
If your perl doesn't know about pack 'q', then you can roll it yourself with the following, but it will be even slower:
use strict;
use warnings;
use Fcntl ':seek';
use bigint;
use constant CHUNK => 65536;
sub calc {
my $file = shift or die "no filename given\n";
my $hash = -s $file;
my $chunk = CHUNK;
$hash < $chunk and die "$file is too small ($hash bytes < $chunk)\
+n";
open my $in, '<', $file or die "Cannot open $file for input: $!\n"
+;
local $/ = \$chunk;
my @val = unpack 'L*' , readline($in);
for (my $j = 0; $j < $#val; $j += 2) {
$hash += ($val[$j] << 32) + $val[$j+1];
$hash &= 2 ** 64 - 1;
}
seek($in, SEEK_END, -$chunk);
@val = unpack 'L*' , readline($in);
for (my $j = 0; $j < $#val; $j += 2) {
$hash += ($val[$j] << 32) + $val[$j+1];
$hash &= 2 ** 64 - 1;
}
close $in;
return sprintf '%016x', $hash;
}
open my $out, '>', 'test.deleteme';
print $out ' ' x (65536*4);
close $out;
print calc('test.deleteme'), $/;
Perl's probably not the best language for this. The code is much shorter than the other languages, which is usually the case for a given algorithm, but the performance is horrible. You really want to do yourself a favour and run this stuff on a 64 bit architecture.
Maybe there's another monk who's into numerical analysis and can spot an insight, but it's beyond my ken.
• another intruder with the mooring in the heart of the Perl
|