in reply to Interleaving bytes in a string quickly

How do
s/.\K/\x01/sg; # 5.010+
s/(?<=.)/\x01/sg; # 5.006+
perform?

Replies are listed 'Best First'.
Re^2: Interleaving bytes in a string quickly
by BrowserUk (Patriarch) on Feb 26, 2010 at 18:08 UTC

    I had to tweak the 5.6 solution a bit:

    ike510 => q[ ( my $out = $in ) =~ s/.\K/\x40/sg; print 'ike510', unpack 'C20', $out if DEBUG;; ], ike5_6 => q[ ( my $out = $in ) =~ s/(.)/$1\x40/sg; print 'ike5.6', unpack 'C20', $out if DEBUG;; ],

    But the answer to the quesion is 'not well':

    Rate ike5_6 ike510 buk shmem salva jmac lanX fletch ike5_6 0.882/s -- -30% -56% -68% -73% -90% -91% -100% ike510 1.26/s 43% -- -37% -54% -61% -85% -88% -100% buk 1.99/s 126% 58% -- -28% -39% -76% -81% -99% shmem 2.77/s 214% 120% 39% -- -15% -67% -73% -99% salva 3.25/s 269% 158% 63% 17% -- -61% -69% -99% jmac 8.42/s 855% 567% 322% 204% 159% -- -19% -97% lanX 10.4/s 1076% 721% 420% 274% 219% 23% -- -97% fletch 309/s 34974% 24395% 15418% 11055% 9407% 3575% 2883% --

    I suspect that constantly reallocating the results string every 8 insertions does for them.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I had to tweak the 5.6 solution a bit

      Why? Your "tweak" made it slower

      $ perl -le'$_="abc"; s/(.)/$1\x40/sg; print' a@b@c@ $ perl -le'$_="abc"; s/(?<=.)/\x40/sg; print' a@b@c@ $ perl -le'$_="abc"; s/(?!^)/\x40/sg; print' a@b@c@ $ perl -e' use Benchmark qw( cmpthese ); our $i = chr(1) x 1e6; cmpthese(-3, { cap => sub { (my $o=our $i) =~ s/(.)/$1\x40/sg; }, peek1 => sub { (my $o=our $i) =~ s/(?<=.)/\x40/sg; }, peek2 => sub { (my $o=our $i) =~ s/(?!^)/\x40/sg; }, }); ' Rate cap peek1 peek2 cap 1.39/s -- -25% -27% peek1 1.85/s 33% -- -3% peek2 1.91/s 37% 4% --

        I guess I must have copy&pasted wrong somehow the first time because it didn't work:

        ike5_6 1100110011001100110011001100110011001100 (warning: too few iterations for a reliable count)

        Done correctly this time is does work, but frankly the difference it makes to performance is minimal:

        C:\test>825494 (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate ike5_6 ike510 buk shmem salva jmac lanX fletch ike5_6 0.298/s -- -10% -72% -79% -82% -93% -95% -100% ike510 0.330/s 11% -- -68% -76% -80% -92% -94% -100% buk 1.05/s 251% 217% -- -26% -37% -75% -81% -99% shmem 1.41/s 371% 325% 34% -- -15% -66% -74% -99% salva 1.66/s 457% 402% 59% 18% -- -60% -70% -99% jmac 4.19/s 1305% 1167% 300% 198% 152% -- -24% -97% lanX 5.51/s 1746% 1566% 426% 292% 232% 31% -- -97% fletch 163/s 54408% 49077% 15428% 11461% 9687% 3780% 2852% --

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.