in reply to Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
Bit ops?
sub using_str_bit_ops_and_s { my ($s1, $s2) = @_; (my $mask = $s1) =~ s/[^\x00]/\xFF/g; return ($s1 & $mask) | ($s2 & ~$mask); }
tr should be faster.
sub using_str_bit_ops_and_tr { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return ($s1 & $mask) | ($s2 & ~$mask); }
Benchmarks:
Rate split1 substr1 bit_ops map_split split1 1.25/s -- -95% -100% -100% substr1 25.5/s 1939% -- -96% -99% bit_ops 642/s 51313% 2421% -- -71% map_split 2230/s 178496% 8657% 247% --
Corion's solution humbles my ass-kicking solution! I wonder if a higher density of zeros would boost my rating?
Update: Added tr variant.
Update: Added Benchmarks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
by bart (Canon) on Sep 12, 2007 at 16:17 UTC |