Results:sub using_vec { my ($s1, $s2) = @_; vec($s1, $_, 8) ||= vec($s2, $_, 8) for 0 .. length($s1)-1; return $s1; } sub using_str_bit_ops_and_s { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return ($s1 & $mask) | ($s2 & ~$mask); } my $x = pack 'C*', map int rand(256), 1 .. 64*1024; my $y = pack 'C*', map 1+int rand(255), 1 .. 64*1024; using_str_bit_ops_and_s($x, $y) eq using_vec($x, $y) or die "Results a +re different??"; use Benchmark 'cmpthese'; cmpthese -3, { using_str_bit_ops_and_s => sub{ my $r = using_str_bit_ops_and_s($x, +$y) }, using_vec=> sub{ my $r = using_vec($x, $y) }, };
12 times slower than ikegami's code I use as a reference implementation.Rate using_vec using_str_bit_o +ps_and_s using_vec 51.8/s -- + -92% using_str_bit_ops_and_s 672/s 1196% + --
In reply to Re: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
by bart
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |