#!/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
|