Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Performance penalty of using qr//

by Eily (Monsignor)
on Dec 21, 2017 at 10:32 UTC ( #1205969=note: print w/replies, xml ) Need Help??


in reply to Performance penalty of using qr//

With this version of use_str:

sub use_str { my $re = qr/^1\d2\d3\d4\d5\d6\d7\d8\d900$/; for (my $n = 1_010_101_030; $n <= 1_389_026_623; ) { # stringify the qr regex my $str = "$re"; my $s = $n * $n; return $n if $s =~ /$str/; # recompiled each time I guess? $n += 40; $s = $n * $n; return $n if $s =~ /$str/; $n += 60; } die; }
I get:
Benchmark: timing 5 iterations of use_qr, use_re, use_str... use_qr: 33 wallclock secs (33.62 usr + 0.00 sys = 33.62 CPU) @ 0 +.15/s (n=5) use_re: 18 wallclock secs (17.14 usr + 0.00 sys = 17.14 CPU) @ 0 +.29/s (n=5) use_str: 25 wallclock secs (24.99 usr + 0.00 sys = 24.99 CPU) @ 0 +.20/s (n=5) s/iter use_qr use_str use_re use_qr 6.72 -- -26% -49% use_str 5.00 35% -- -31% use_re 3.43 96% 46% -- Appuyez sur une touche pour continuer...
So this looks like perl is doing even worse with qr than it would by recompiling the stringified version on each iteration (unless I'm missing some optimization, but I tried adding dummy logic like rand(100) < 100 to prevent perl from noticing that the expression are constants, and didn't see significant changes)?

Edit: perl v5.20 here BTW

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1205969]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-04-21 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?