Perlについての質問箱 46箱目
■ このスレッドは過去ログ倉庫に格納されています
0310デフォルトの名無しさん
2011/03/10(木) 19:31:01.28use utf8;
use Benchmark qw(:all);
my $text = 'あるないないあるあるある';
cmpthese ( timethese( undef, {
substr => sub {
my $count;
my $word_len = length 'ある';
for my $i ( 0 .. length($text) - $word_len ) {
$count++ if substr($text, $i, $word_len) eq 'ある';
}},
bytes => sub {
use bytes;
my $count;
my $word_len = length 'ある';
for my $i ( 0 .. length($text) - $word_len ) {
$count++ if substr($text, $i, $word_len) eq 'ある';
}},
re_list => sub {
my $count = @{[$text =~ m{ある}g]};
},}));
Benchmark: running bytes, re_list, substr for at least 3 CPU seconds...
bytes: 2 wallclock secs ( 3.13 usr + 0.01 sys = 3.13 CPU) @ 21894.42/s (n=68639)
re_list: 4 wallclock secs ( 3.10 usr + 0.02 sys = 3.12 CPU) @ 58206.79/s (n=181838)
substr: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 38457.81/s (n=116681)
Rate bytes substr re_list
bytes 21894/s -- -43% -62%
substr 38458/s 76% -- -34%
re_list 58207/s ..166% 51% --
■ このスレッドは過去ログ倉庫に格納されています