LoginSignup
0
0

More than 5 years have passed since last update.

Perl で ベンチマーク取得

Posted at
use strict;
use warnings;
use Benchmark qw/timethese cmpthese/;

my $hoge1    = qq(hoge) x 1;
my $hoge10   = qq(hoge) x 10;
my $hoge100  = qq(hoge) x 100;
my $hoge1000 = qq(hoge) x 1000;

# 計測したい項目を設定
my $bm1 = timethese(100000, 
{
    equal_hoge1     => sub { $hoge1    eq $hoge1       for(1..100) },
    regex_hoge1     => sub { $hoge1    =~ /$hoge1/     for(1..100) },
    regexo_hoge1    => sub { $hoge1    =~ /$hoge1/o    for(1..100) },

    equal_hoge10    => sub { $hoge10   eq $hoge10      for(1..100) },
    regex_hoge10    => sub { $hoge10   =~ /$hoge10/    for(1..100) },
    regexo_hoge10   => sub { $hoge10   =~ /$hoge10/o   for(1..100) },

    equal_hoge100   => sub { $hoge100  eq $hoge100     for(1..100) },
    regex_hoge100   => sub { $hoge100  =~ /$hoge100/   for(1..100) },
    regexo_hoge100  => sub { $hoge100  =~ /$hoge100/o  for(1..100) },

    equal_hoge1000  => sub { $hoge1000 eq $hoge1000     for(1..100) },
    regex_hoge1000  => sub { $hoge1000 =~ /$hoge1000/   for(1..100) },
    regexo_hoge1000 => sub { $hoge1000 =~ /$hoge1000/o  for(1..100) },

});

# 結果出力
cmpthese $bm1;

# ->
# Benchmark: timing 100000 iterations of equal_hoge1, equal_hoge10, equal_hoge100, equal_hoge1000, regex_hoge1, regex_hoge10, regex_hoge100, regex_hoge1000, regexo_hoge1, regexo_hoge10, regexo_hoge100, regexo_hoge1000...
# equal_hoge1:      0 wallclock secs ( 0.56 usr +  0.00 sys =  0.56 CPU) @ 178253.12/s (n=100000)
# equal_hoge10:     1 wallclock secs ( 0.59 usr +  0.00 sys =  0.59 CPU) @ 168634.06/s (n=100000)
# equal_hoge100:    1 wallclock secs ( 0.59 usr +  0.00 sys =  0.59 CPU) @ 168634.06/s (n=100000)
# equal_hoge1000:   0 wallclock secs ( 0.59 usr +  0.00 sys =  0.59 CPU) @ 168634.06/s (n=100000)
# regex_hoge1:      2 wallclock secs ( 1.45 usr +  0.00 sys =  1.45 CPU) @ 68917.99/s (n=100000)
# regex_hoge10:     2 wallclock secs ( 1.98 usr +  0.00 sys =  1.98 CPU) @ 50505.05/s (n=100000)
# regex_hoge100:    5 wallclock secs ( 4.80 usr +  0.00 sys =  4.80 CPU) @ 20811.65/s (n=100000)
# regex_hoge1000:  32 wallclock secs (32.54 usr +  0.00 sys = 32.54 CPU) @ 3072.95/s (n=100000)
# regexo_hoge1:     2 wallclock secs ( 1.31 usr +  0.00 sys =  1.31 CPU) @ 76335.88/s (n=100000)
# regexo_hoge10:    2 wallclock secs ( 1.72 usr +  0.00 sys =  1.72 CPU) @ 58275.06/s (n=100000)
# regexo_hoge100:   4 wallclock secs ( 4.31 usr +  0.00 sys =  4.31 CPU) @ 23223.41/s (n=100000)
# regexo_hoge1000: 30 wallclock secs (30.20 usr +  0.00 sys = 30.20 CPU) @ 3311.04/s (n=100000)
#
#                     Rate regex_hoge1000 regexo_hoge1000 regex_hoge100 regexo_hoge100 regex_hoge10 regexo_hoge10 regex_hoge1 regexo_hoge1 equal_hoge100 equal_hoge10 equal_hoge1000 equal_hoge1
# regex_hoge1000    3073/s             --             -7%          -85%           -87%         -94%          -95%        -96%         -96%          -98%         -98%           -98%        -98%
# regexo_hoge1000   3311/s             8%              --          -84%           -86%         -93%          -94%        -95%         -96%          -98%         -98%           -98%        -98%
# regex_hoge100    20812/s           577%            529%            --           -10%         -59%          -64%        -70%         -73%          -88%         -88%           -88%        -88%
# regexo_hoge100   23223/s           656%            601%           12%             --         -54%          -60%        -66%         -70%          -86%         -86%           -86%        -87%
# regex_hoge10     50505/s          1544%           1425%          143%           117%           --          -13%        -27%         -34%          -70%         -70%           -70%        -72%
# regexo_hoge10    58275/s          1796%           1660%          180%           151%          15%            --        -15%         -24%          -65%         -65%           -65%        -67%
# regex_hoge1      68918/s          2143%           1981%          231%           197%          36%           18%          --         -10%          -59%         -59%           -59%        -61%
# regexo_hoge1     76336/s          2384%           2205%          267%           229%          51%           31%         11%           --          -55%         -55%           -55%        -57%
# equal_hoge100   168634/s          5388%           4993%          710%           626%         234%          189%        145%         121%            --           0%             0%         -5%
# equal_hoge10    168634/s          5388%           4993%          710%           626%         234%          189%        145%         121%            0%           --             0%         -5%
# equal_hoge1000  168634/s          5388%           4993%          710%           626%         234%          189%        145%         121%            0%           0%             --         -5%
# equal_hoge1     178253/s          5701%           5284%          757%           668%         253%          206%        159%         134%            6%           6%             6%          --
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0