qw。
ども。お寒ぅございます。
収入印紙を金券ショップで買って、320円得した あに山です。
配列の値を設定する際に、気軽に qw を使ってるけど
処理速度に影響ないのかなぁ…と思ってたので
軽くテストしたメモです。
#!/usr/bin/perl -wuse strict;
use Benchmark qw(:all);
my @testArray = ();
my $result = timethese(100000, {
testA => sub { @testArray = ('a', 'b', 'c', 'd', 'e', 'f', 'g'); },
testB => sub { @testArray = ("a", "b", "c", "d", "e", "f", "g"); },
testC => sub { @testArray = qw(a b c d e f g); },
testD => sub { @testArray = qw/a b c d e f g/; }
});
cmpthese($result);
exit;
Benchmark: timing 100000 iterations of testA, testB, testC, testD...まぁ、思った通り、あんまり違いはないですね。testA: 1 wallclock secs ( 2.30 usr + 0.00 sys = 2.30 CPU) @ 43535.05/s (n=100000)
testB: 2 wallclock secs ( 2.31 usr + 0.00 sys = 2.31 CPU) @ 43233.90/s (n=100000)
testC: 2 wallclock secs ( 2.30 usr + 0.00 sys = 2.30 CPU) @ 43535.05/s (n=100000)
testD: 2 wallclock secs ( 2.31 usr + 0.00 sys = 2.31 CPU) @ 43233.90/s (n=100000)
Rate testB testD testC testA
testB 43234/s -- -0% -1% -1%
testD 43234/s 0% -- -1% -1%
testC 43535/s 1% 1% -- -0%
testA 43535/s 1% 1% 0% --
安心して引き続き、qw を使いたいと思います。
続いて、文字列を変数に代入するのに qq を使うのは、
遅いんじゃないかなぁ…と思ってたので、その辺りのテスト。
ヒアドキュメントなんかも、ついでに使って候補に入れてみる。
#!/usr/bin/perl -wuse strict;
use Benchmark qw(:all);
my $value = 'ddd';
my $testString = '';
my $result = timethese(100000, {
testA => sub {
$testString = "aaa";
$testString .= "bbb";
$testString .= "ccc${value}";
},
testB => sub {
$testString = "aaa"
. "bbb"
. "ccc${value}";
},
testC => sub {
$testString = "aaa"
. "bbb"
. "ccc"
. $value;
},
testD => sub {
$testString = 'aaa';
$testString .= 'bbb';
$testString .= 'ccc';
$testString .= $value;
},
testE => sub {
$testString = 'aaa'
. 'bbb'
. 'ccc'
. $value;
},
testF => sub {
$testString = qq{aaa};
$testString .= qq{bbb};
$testString .= qq{ccc$value};
},
testG => sub {
$testString = qq{aaa};
$testString .= qq{bbb};
$testString .= qq{ccc};
$testString .= $value;
},
testH => sub {
$testString =<<__EOT__;
aaabbbccc${value}
__EOT__
}
});
cmpthese($result);
exit;
Benchmark: timing 100000 iterations of testA, testB, testC, testD, testE, testF, testG, testH...testA: 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1282051.28/s (n=100000)
(warning: too few iterations for a reliable count)
testB: -1 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU) @ 1612903.23/s (n=100000)
(warning: too few iterations for a reliable count)
testC: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 3125000.00/s (n=100000)
(warning: too few iterations for a reliable count)
testD: 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1282051.28/s (n=100000)
(warning: too few iterations for a reliable count)
testE: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 3125000.00/s (n=100000)
(warning: too few iterations for a reliable count)
testF: 1 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1282051.28/s (n=100000)
(warning: too few iterations for a reliable count)
testG: 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1282051.28/s (n=100000)
(warning: too few iterations for a reliable count)
testH: 0 wallclock secs ( 0.05 usr + 0.00 sys = 0.05 CPU) @ 2127659.57/s (n=100000)
(warning: too few iterations for a reliable count)
Rate testA testD testF testG testB testH testE testC
testA 1282051/s -- 0% -0% -0% -21% -40% -59% -59%
testD 1282051/s 0% -- -0% -0% -21% -40% -59% -59%
testF 1282051/s 0% 0% -- 0% -21% -40% -59% -59%
testG 1282051/s 0% 0% 0% -- -21% -40% -59% -59%
testB 1612903/s 26% 26% 26% 26% -- -24% -48% -48%
testH 2127660/s 66% 66% 66% 66% 32% -- -32% -32%
testE 3125000/s 144% 144% 144% 144% 94% 47% -- -0%
testC 3125000/s 144% 144% 144% 144% 94% 47% 0% --
まぁ、これまた案の定ですが、 qq 使った testF は遅めです。
qq 以外の何度も代入してる、testA, testD, testG も遅い組に入ってますが
この辺りも予想通り。
ヒアドキュメントは、まぁまぁだけど、何度もテストしてみると
たまに極端に遅くなる時があったりなんかして。