There seems not to be a threshold value at which it kicks in. This suggests it's not COW related?.

Modifying the code to take an argument to use as a power of 2 gives these results on Strawberry Perl 5.38. Code is below inside readmore tags.

A doubling of the n leads to a more than doubling of the time on my machine. One would need to run it many times to be confident in the true rate of change but these numbers are close to a linear relationship when plotted with time log scaled. (For those interested in such things, the power function fitted using MS Excel is t = 8E-28**(n*21.743).)

Edit: n in this case is the argument to the script, so the x-axis is n, not 2**n.

Edit 2: And a polynomial function gives a better fit when using actual number of repetitions: t = 2E-11*n**2 - 5E-06*n + 0.387, with R^2 = 0.9989 (indicative-only given the sample size).

C:\user\perlmonks>perl 11153747.pl 15 32768 0.0403292179107666 0.0388741493225098 v5.38.0 C:\user\perlmonks>perl 11153747.pl 16 65536 0.0916790962219238 0.0764880180358887 v5.38.0 C:\user\perlmonks>perl 11153747.pl 17 131072 0.361366987228394 0.142138957977295 v5.38.0 C:\user\perlmonks>perl 11153747.pl 18 262144 1.22035908699036 0.37365198135376 v5.38.0 C:\user\perlmonks>perl 11153747.pl 19 524288 4.05149698257446 0.628846883773804 v5.38.0 C:\user\perlmonks>perl 11153747.pl 20 1048576 21.3440728187561 3.00999999046326 v5.38.0

Code:

use strict; use warnings; use feature 'say'; use Time::HiRes 'time'; my $n = 2**$ARGV[0]; say $n; my $x = 'a' x ($n-1); my $y = 'b' . $x; my $t = time; $x =~ s/./$&-/g; say time - $t; $t = time; $y =~ s/./$&-/g; say time - $t; say $^V; __END__

In reply to Re^5: Substitution unexpectedly very slow, in Strawberry by swl
in thread Substitution unexpectedly very slow, in Strawberry by Anonymous Monk

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.