in reply to Speed Improvement

UPDATE - turns out study is a no-op since 5.16.0. Not sure why this is not noted in the documentation. Perhaps because this was always a try-it-and-see kind of thing. So, if you're on versions earlier than that, it might help. In any event, it would not help in this particular application.

See if study might help. It is intended to speed searches within a string.

1 Peter 4:10

Replies are listed 'Best First'.
Re^2: Speed Improvement
by dave_the_m (Monsignor) on Dec 02, 2014 at 11:24 UTC
    Note that study() has been a no-op since 5.16.0

    Dave.

      UPDATE: typo inflates the results here, study does not produce the improvement shown here. But it does do something, not nothing, so I'm pretty sure it's not a no-op.

      C:\perl>perl -v This is perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x +86-multi-thread-64int
      #!/usr/bin/perl use strict; use warnings; use 5.010; use Benchmark qw(cmpthese); my @messages = <DATA>; cmpthese(1000000, { 'mca_func' => sub { my $out = mca_substitute($_) foreach (@messages); }, 'toolic_func' => sub { my $out = toolic_substitute($_) foreach (@messages); }, 'study_func' => sub { my $out = study_substitute($_) for each (@messages); }, }); sub toolic_substitute { my $message = shift; $message =~ s/\{\\d(\d+)\}/sprintf "%0${1}d", int(rand(10**$1))/ge +; return $message; } sub study_substitute { my $message = shift; study $message; $message =~ s/\{\\d(\d+)\}/sprintf "%0${1}d", int(rand(10**$1))/ge +; return $message; } sub mca_substitute { my $message = shift; $message =~ s/\{\\d(\d+)\}/myreplace($1)/ge; return $message; } sub myreplace { return '' unless $_[0]; my $string = ''; $string .= int(rand 10) for (1..$_[0]); return $string; } __DATA__ This is a message! {\d3} --- {\d2} Another {\d3} Message with {\d5} A {\d1} little {\d6} longer string {\d3}
      C:\perl>perl testpm.pl Rate mca_func toolic_func study_func mca_func 34744/s -- -32% -78% toolic_func 50795/s 46% -- -68% study_func 157505/s 353% 210% -- C:\perl>

      Not bad for a no-op.

      1 Peter 4:10

        I think you may be being fooled by an error:

        my $out = study_substitute($_) for each (@messages); #..................................^^^?^^^^

        FWIW: I don't see any improvement (actually a 2% drop) in the performance of the study version (using 5.10/win).


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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 stop working now in the hope that my no-op will result in such a gain in performance... :-)

        Thank you for that addition.

        McA