Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Speed Improvement

by GotToBTru (Prior)
on Dec 02, 2014 at 15:20 UTC ( [id://1108969]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Speed Improvement
in thread Speed Improvement

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

Replies are listed 'Best First'.
Re^4: Speed Improvement
by BrowserUk (Patriarch) on Dec 02, 2014 at 15:37 UTC

    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 got the same results once I fixed the typo: yours and McA's algorithms performed slightly worse, toolic's only marginally better when study was added. So something happened, definitely not a no-op, but nothing useful either! With such short strings, I shouldn't be surprised.

      1 Peter 4:10
        yours and McA's algorithms performed slightly worse,

        studying takes time and rarely yields enough extra performance to cover the cost, unless you are searching very long strings -- eg. whole files -- repeatedly. Ie. study once; search many times.

        definitely not a no-op,

        I think if you look back at Dave_the_M's post you'll see that he said "a noop since 5.16". (I'm assuming the use 5.10; is indicative of what you are using?)


        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.
        [...] performed slightly worse, toolic's only marginally better [....]definitely not a no-op

        You seem to be unaware that Benchmark's attempts to subtract "overhead" from its calculations means that differences of about 20% or less are likely to have absolutely no meaning (especially for ridiculously short operations for which optimizing is almost always just a waste of your time, such as the operations y'all have been optimizing). You can easily get a report of a 20% performance difference after just changing the names you used so they get run in a different order or just running it enough times to get a big enough sample.

        I will be genuinely shocked if somebody produces something useful that uses this operation where the reported up-to 300% performance improvement actually leads to a noticeable change in total script run-time (where "noticeable" means "over 20%").

        The line from the original question:

        Every bit of speed I can squeeze out of this sub will help!

        just made me laugh. :)

        - tye        

Re^4: Speed Improvement
by McA (Priest) on Dec 02, 2014 at 15:39 UTC

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found