Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Substitution unexpectedly very slow, in Strawberry

by Anonymous Monk
on Aug 06, 2023 at 20:17 UTC ( [id://11153747]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use feature 'say'; use Time::HiRes 'time'; my $x = 'a' x 1e6; my $y = 'b' . $x; my $t = time; $x =~ s/./$&-/g; say time - $t; $t = time; $y =~ s/./$&-/g; say time - $t; say $^V; __END__ 12.7103209495544 0.850119113922119 v5.32.1 12.7171239852905 0.765337944030762 v5.28.2 0.345196962356567 0.314804077148438 v5.26.3

I didn't try latest (are they still beta?) versions, was this issue fixed? Online tests (Perl on Linux, of course) don't exhibit any anomaly. Looks like at some time after 5.26 version something weird happened to Strawberry Perl. Hard to imagine just _what_ can be happening for 10+ seconds with such relatively short string, 1 core fully loaded. And why for the 1st time only.

I understand s/.\K/-/g is better (no speed anomaly if written as such), it's not the question.

Replies are listed 'Best First'.
Re: Substitution unexpectedly very slow, in Strawberry
by choroba (Cardinal) on Aug 06, 2023 at 20:50 UTC
    Just a comparison. $^O eq 'linux':
    0.254748821258545 0.255423069000244 v5.26.1 0.514794111251831 0.481626033782959 v5.37.3

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Substitution unexpectedly very slow, in Strawberry -- issue submitted
by Discipulus (Canon) on Aug 10, 2023 at 08:43 UTC
    Hello all,

    while I dont catch details involved, I care about perl to be lovely maintained and I know where to ask.. :)

    After knocking various #irc channels in #p5p I found the kind attention of xenu who digged this a bit deeply and opened Perl/perl5/issues/21360.

    A big ++ to xenu and to you all!

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Substitution unexpectedly very slow, in Strawberry
by swl (Parson) on Aug 06, 2023 at 22:21 UTC

    Still occurs with Strawberry Perl 5.36.1 and 5.38.0 (see below) although I suspect it is more generic than Strawberry Perl.

    Running the code through nytprof shows 1000001 calls to main::CORE:substcont at line 10 take most of the time.

    It does not happen when the $x repetition is set to 1e5. There was a possibly related thread here a few months ago about character repetitions and COW but I can't locate it right now.

    0.224835157394409 0.228233814239502 v5.22.3 9.92650699615479 0.662662982940674 v5.28.0 8.49285101890564 0.744230985641479 v5.36.1 8.02936911582947 0.788676977157593 v5.38.0
      There was a possibly related thread here a few months ago about character repetitions and COW but I can't locate it right now.

      It would be interesting to compare with a perl that was built without the COW.
      How might that be achieved on Windows ? (In fact, how is it even done on Linux ? IIRC it's fairly easily do-able on *nix but I can't find the relevant documentation.)

      On Windows, I'm seeing the same thing, irrespective of whether perl-5.38.0 is built with a mingw-w64 port of gcc or with Microsoft's Visual Studio 2022. Unthreaded builds of perl fare just as poorly as the multi-thread builds.
      The one thing that does make a big difference is to use a 32-bit build of perl.
      For example, with a 64-bit MSVC-built perl-5.38.0:
      >perl time.pl 6.13913488388062 0.530965089797974 v5.38.0
      For a 32-bit perl-5.38.0 built using the same MSVC compiler (VS2022) in 32-bit mode:
      >perl time.pl 0.0664510726928711 0.0533270835876465 v5.38.0
      But even with that 32-bit build of perl, the same issue becomes evident when "1e6" is changed to "2e6":
      >perl time.pl 6.67774796485901 1.07769107818604 v5.38.0
      Cheers,
      Rob

        It also might not be related to COW and instead be something in the regex engine that is specific to windows. It's probably worth flagging with p5p.

Re: Substitution unexpectedly very slow, in Strawberry
by Discipulus (Canon) on Aug 07, 2023 at 07:26 UTC
    More strawberry tastes :)

    | 1.23387217521667 | 0.566806077957153 | v5.12.3 [OK] \perl5.12-32bit\perl\bin\perl.exe | 0.269863128662109 | 0.27295708656311 | v5.20.3 [OK] \perl5.20.64bit\perl\bin\perl.exe | 0.262228012084961 | 0.269136905670166 | v5.22.3 [OK] \perl5.22.64bit\perl\bin\perl.exe | 0.294710159301758 | 0.301361799240112 | v5.24.2 [OK] \perl5.24.64bit\perl\bin\perl.exe | 0.276448965072632 | 0.271692037582397 | v5.26.0 [OK] \perl5.26.64bit\perl\bin\perl.exe | 0.282638788223267 | 0.280515909194946 | v5.26.2 [OK] \perl-5.26.64bit-PDL\perl\bin\perl.exe | 3.31176686286926 | 0.708863019943237 | v5.28.0 [OK] \perl5.28.32bit\perl\bin\perl.exe | 11.2146329879761 | 0.633802890777588 | v5.28.1 [OK] \perl5.28-64bit\perl\bin\perl.exe | 11.6474039554596 | 0.678498983383179 | v5.32.0 [OK] \perl5.32.64bit\perl\bin\perl.exe

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11153747]
Approved by jo37
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found