in reply to Re^4: "advanced" Perl functions and maintainability
in thread "advanced" Perl functions and maintainability
I've done the benchmarks before. Here:
#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; my $string = "this is a string" x 300; my $short_string = "this is a short string"; cmpthese(10000000, { 'index' => sub { my $res; $res = index($string, "this", 0); $res = index($string, "string"); $res = rindex($string, "string"); }, regex => sub { my $res; $res = $string =~ /^this/; $res = $string =~ /string/; $res = $string =~ /string$/; }, shortindex => sub { my $res; $res = index($short_string, "this", 0); $res = index($short_string, "short"); $res = rindex($short_string, "string"); }, shortregex => sub { my $res; $res = $short_string =~ /^this/; $res = $short_string =~ /short/; $res = $short_string =~ /string$/; }, substrindex => sub { my $res; my $substr = "this is"; $res = index($short_string, $substr); }, substrregex => sub { my $res; my $substr = "this is"; $res = $short_string =~ /\Q$substr\E/; } });
Benchmark: timing 10000000 iterations of index, regex, shortindex, sho +rtregex, substrindex, substrregex... index: 18 wallclock secs (17.29 usr + 0.00 sys = 17.29 CPU) @ 57 +8369.00/s (n=10000000) regex: 32 wallclock secs (32.52 usr + 0.00 sys = 32.52 CPU) @ 30 +7503.08/s (n=10000000) shortindex: 15 wallclock secs (15.15 usr + 0.00 sys = 15.15 CPU) @ 66 +0066.01/s (n=10000000) shortregex: 26 wallclock secs (27.14 usr + 0.00 sys = 27.14 CPU) @ 36 +8459.84/s (n=10000000) substrindex: 9 wallclock secs ( 9.39 usr + 0.00 sys = 9.39 CPU) @ 1 +064962.73/s (n=10000000) substrregex: 17 wallclock secs (16.64 usr + 0.00 sys = 16.64 CPU) @ 6 +00961.54/s (n=10000000) Rate regex shortregex index substrregex shortindex s +ubstrindex regex 307503/s -- -17% -47% -49% -53% + -71% shortregex 368460/s 20% -- -36% -39% -44% + -65% index 578369/s 88% 57% -- -4% -12% + -46% substrregex 600962/s 95% 63% 4% -- -9% + -44% shortindex 660066/s 115% 79% 14% 10% -- + -38% substrindex 1064963/s 246% 189% 84% 77% 61% + --
The regex is almost always slower, but usually not by that much.
janitored by ybiC: Replaced almost-allways-inappropriate <pre> tags around benchmark results with <code> tags, to avoid annoying lateral scrolling
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: "advanced" Perl functions and maintainability
by Anonymous Monk on Dec 13, 2004 at 14:13 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 15:11 UTC | |
by itub (Priest) on Dec 13, 2004 at 16:37 UTC | |
by Anonymous Monk on Dec 13, 2004 at 15:43 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 16:19 UTC | |
by diotalevi (Canon) on Dec 13, 2004 at 21:27 UTC | |
|
Re^6: "advanced" Perl functions and maintainability
by Aristotle (Chancellor) on Dec 20, 2004 at 03:51 UTC |