Donald J Christensen <dj...@ci...> writes:
> Ken Yap wrote:
> >
> > >> Your code runs in 0.1s on a 1MB vmlinuz file on my system (350MHz K6-2).
> > >> Congratulations.
> > >
> > >Nice. Then I won't worry about the code to much.
> >
> > I should add that the 0.1s included the time to read in the file and
> > mknbi has to do this anyway.
> >
> > >Nice. Do you know if the substr hurts? I guess at 0.1s it isn't too
> > >bad. Last time I was messing with the code and I complained checksums
> > >in perl were terribly slow, you speculated that the problem was that I
> > >was using substr. And it has taken me a long time to think of a
> > >substitute.
> >
> > I don't see any other reasonable way to do it. You're only calling it as
> > many times as there are 64k chunks so it's not a significant overhead I
> > think.
>
> Eric,
>
> Based on Ken's comments about needing to load the file in anyway, it occurs
> to me that maybe you could integrate the checksum code with the code that
> loads the file in. If you read in the file in 64K chunks, you could checksum
> each chunk. If you do this, benchmark it against the current version and dump
> it if it isn't substantially faster (and maybe dump it anyway in the interest
> of readability if it only takes 0.1s).
Currently mknbi does not integrate this checksum code. So comparing
against the current version doesn't make much sense in that context.
Running the code while the data is in memory certainly make sense.
For completeness my previous terribly slow implemention was:
sub compute_ip_checksum
{
my ($str) = @_;
my ($checksum, $i, $size, $shorts);
$checksum = 0;
$size = length($str);
$shorts = $size >> 1;
for($i = 0; $i < $shorts; $i++) {
$checksum += unpack('S', substr($str, $i <<1, 2));
$checksum -= 0xFFFF unless ($checksum <= 0xFFFF);
}
if ($size & 1) {
$checksum -= 0xFFFF unless ($checksum <= 0xFFFF);
$checksum += unpack('C', substr($str, -1, 1));
}
return (~$checksum) & 0xFFFF;
}
Which is slow even if you remove the body of the loop.
>
> It has been a long while since I looked at mknbi, so this idea might not
> come anywhere close to useful. But since I had it, I thought I would
> share it. :^)
Always useful. And one of the points of a public discussion.
Eric
|