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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Speed Improvement
by BrowserUk (Patriarch) on Dec 02, 2014 at 15:37 UTC | |
by GotToBTru (Prior) on Dec 02, 2014 at 16:01 UTC | |
by BrowserUk (Patriarch) on Dec 02, 2014 at 16:24 UTC | |
by GotToBTru (Prior) on Dec 02, 2014 at 16:33 UTC | |
by dave_the_m (Monsignor) on Dec 02, 2014 at 16:50 UTC | |
by BrowserUk (Patriarch) on Dec 02, 2014 at 16:47 UTC | |
by tye (Sage) on Dec 03, 2014 at 08:07 UTC | |
by Corion (Patriarch) on Dec 03, 2014 at 08:14 UTC | |
by McA (Priest) on Dec 03, 2014 at 08:40 UTC | |
by tye (Sage) on Dec 03, 2014 at 09:19 UTC | |
by BrowserUk (Patriarch) on Dec 03, 2014 at 17:31 UTC | |
by tye (Sage) on Dec 03, 2014 at 20:00 UTC | |
| |
by BrowserUk (Patriarch) on Dec 03, 2014 at 16:14 UTC | |
|
Re^4: Speed Improvement
by McA (Priest) on Dec 02, 2014 at 15:39 UTC |