in reply to Re: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
Bit ops?I was thinking in the same direction, except I'd use xor. After all, ((A xor B ) and 0) xor B is B, and ((A xor B) and true) xor B is A, for any value of A and B.sub using_str_bit_ops_and_s { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return ($s1 & $mask) | ($s2 & ~$mask); }
So:
sub using_str_bit_xor { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return (($s1 ^ $s2) & $mask ) ^ $s2; }
Benchmarks: (and yes I did use 64k byte strings):
Result:my $x = pack 'C*', map int rand(256), 1 .. 64*1024; my $y = pack 'C*', map int rand(256), 1 .. 64*1024; using_str_bit_ops_and_s($x, $y) eq using_str_bit_xor($x, $y) or die "Results are different??"; use Benchmark 'cmpthese'; cmpthese -3, { using_str_bit_ops_and_s => sub{ my $r = using_str_bit_ops_and_s($x, +$y) }, using_str_bit_xor=> sub{ my $r = using_str_bit_xor($x, $y) }, };
The speed gain is humble, but real.Rate using_str_bit_ops_and_s using_str_ +bit_xor using_str_bit_ops_and_s 686/s -- + -16% using_str_bit_xor 821/s 20% + --
|
|---|