You picked the wrong one. moritz'es solution is broken. Its easy to be fast if you don't have to be correct. :-) (Hint: what happens if the first character in the string is \0?)

Seriously if you need to do this then use one of the XS variants. It will be fastest, however proving that it is might be difficult, especially when working with such large strings. Certainly I wouldnt trust Benchmark.pm with the inline modification variants, especially not the XS ones.

sub moritz { my ($s1, $s2) = @_; my $pos = 0; while ( 0 < ( $pos = index $$s1, "\000", $pos ) ) { substr( $$s1, $pos, 1 ) = substr( $$s2, $pos, 1 ); } } my $s1="\0x\0z"; my $s2="ABCD"; moritz(\$s1,\$s2); print $s1 ne 'AxCz' && 'not ','ok';

The code would be correct if it was

sub moritz { my ($s1, $s2) = @_; my $pos = 0; while ( -1 < ( $pos = index $$s1, "\000", $pos ) ) { substr( $$s1, $pos, 1 ) = substr( $$s2, $pos, 1 ); } }

But then its possible the benchmark results would change as the original isnt doing *anything* on a string that starts with "\0"

---
$world=~s/war/peace/g


In reply to Re: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by demerphq
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.