frei

旧「anney's room」からブログ「frei」だけ引っ越し&残しました

速いグルグルとか。

気にはなってた簡単なテスト。

配列とハッシュの処理速度比較。

配列だと、どんだけ速いんかなぁ…と思ってたから。

#!/usr/bin/perl -w

use strict;

use Benchmark qw(:all);

my @TEST_VALUES = qw(a b c d e f g h i j k l m n);

my $result = timethese(100000, {

testArray => sub {

my @ARRAY = ();

for (@TEST_VALUES) {

push @ARRAY, $_;

}

for (my $i = 0; $i <= $#ARRAY; $i++) {

print "ARRAY:$i = $ARRAY[$i]\n";

}

},

testHash => sub {

my %HASH = ();

my $key = 0;

for (@TEST_VALUES) {

$HASH{$key} = $_;

$key++;

}

for (my $i = 0; $i < $key; $i++) {

print "HASH:$i = $HASH{$i}\n";

}

}

});

cmpthese($result);

exit;

testArray: 7 wallclock secs ( 6.40 usr + 0.30 sys = 6.70 CPU) @ 14925.37/s (n=100000)

testHash: 8 wallclock secs ( 8.73 usr + 0.21 sys = 8.94 CPU) @ 11185.68/s (n=100000)

Rate testHash testArray

testHash 11186/s -- -25%

testArray 14925/s 33% --

…という訳で、テストしたものの「あぁ、ぼんやり速いのね」で終わってしもた。

ちなみに print 文を省くと、もっと差が出る。

Benchmark: timing 100000 iterations of testArray, testHash...

testArray: 3 wallclock secs ( 1.12 usr + 1.41 sys = 2.53 CPU) @ 39525.69/s (n=100000)

testHash: 4 wallclock secs ( 3.66 usr + 0.83 sys = 4.49 CPU) @ 22271.71/s (n=100000)

Rate testHash testArray

testHash 22272/s -- -44%

testArray 39526/s 77% --

ついでに、これも気になってた、ループ中に出てくる変数を

どこでレキシカル宣言するのが速いのか?っちゅーテスト。

実は12月28日にはやってたんだけど、29日に首痛めた影響で

その後、放置してあったのでした。

#!/usr/bin/perl -w

use strict;

use Benchmark qw(:all);

my $string = "\t" x 10000;

my $result = timethese(10000, {

testA => sub {

my $target = 0;

foreach $target (split /\t/, $string) {

print "$target\n";

}

},

testB => sub {

foreach my $target (split /\t/, $string) {

print "$target\n";

}

},

testC => sub {

foreach (split /\t/, $string) {

print "$_\n";

}

},

testD => sub {

foreach (split /\t/, $string) {

my $target = $_;

print "$target\n";

}

},

});

cmpthese($result);

exit;

結果、当然断トツで testC が速いかと思いきや、そうでもなかったり…。

testD が意外に速いのが、よくわからん。

とにかく、testA 形式は、よろしくないっぽい。残念。

Benchmark: timing 10000 iterations of testA, testB, testC, testD...

testA: 66 wallclock secs (35.70 usr + 30.05 sys = 65.75 CPU) @ 152.09/s (n=10000)

testB: 61 wallclock secs (32.30 usr + 28.64 sys = 60.94 CPU) @ 164.10/s (n=10000)

testC: 62 wallclock secs (31.01 usr + 30.81 sys = 61.82 CPU) @ 161.76/s (n=10000)

testD: 61 wallclock secs (29.68 usr + 30.85 sys = 60.53 CPU) @ 165.21/s (n=10000)

Rate testA testC testB testD

testA 152/s -- -6% -7% -8%

testC 162/s 6% -- -1% -2%

testB 164/s 8% 1% -- -1%

testD 165/s 9% 2% 1% --

念の為、何度かテストしても、

やっぱり testA だけは、遅いって事がわかったのでした。ガッカリ。

Benchmark: timing 10000 iterations of testA, testB, testC, testD...

testA: 68 wallclock secs (30.89 usr + 37.54 sys = 68.43 CPU) @ 146.13/s (n=10000)

testB: 67 wallclock secs (28.14 usr + 39.52 sys = 67.66 CPU) @ 147.80/s (n=10000)

testC: 67 wallclock secs (31.42 usr + 35.72 sys = 67.14 CPU) @ 148.94/s (n=10000)

testD: 61 wallclock secs (26.03 usr + 35.46 sys = 61.49 CPU) @ 162.63/s (n=10000)

Rate testA testB testC testD

testA 146/s -- -1% -2% -10%

testB 148/s 1% -- -1% -9%

testC 149/s 2% 1% -- -8%

testD 163/s 11% 10% 9% --