On Wed, 19 Apr 2006 07:42:43 +0200, Helge Jensen
<he**********@slog.dk> wrote:
remember checking the length of b2 also, especially since this is unsafe
code. I would like the function to start with something like:
Very true.
How much, and how have you measured that the above is faster, than simply
I had done some basic measurements earlier, but since you asked I got
a little more curious and did some more detailed testing including
what happens if the arrays doesn't match in the first few bytes of the
comparison.
These are simple timings. Ratios measured as Unsafe:Safe
match 1000 bytes 1 : 3.9
match 20 bytes 1 : 2.1
match 4 bytes 1 : 1.05
1st byte fail 1 : 0.45
2nd byte fail 1 : 0.6
3rd byte fail 1 : 0.7
4th byte fail 1 : 0.9
5th byte fail 1 : 0.95
When large parts of big arrays has to be compared the difference in
performance approaches 4. This is pretty much expected since the byte
version has to loop and compare 4 times as much.
With byte arrays of size 20 the unsafe version is still twice as fast.
With a really small array, and/or if the comparisons fail in the first
few bytes the advantage turns to the safe version. If the first byte
fails the safe version is more than twice as fast. The overhead of the
unsafe version is too large.
I also can't explain why the "match 4 bytes" differs slightly from
"4th byte fail". From just looking at the code I expected them to
perform the same.
--
Marcus Andrén