配列からリファレンスを取得する方法を考え直す
@arrayという配列があった場合、大抵は¥@arrayという風にするのですが、私はよく[@array]という書き方をします。
なんとなくそっちの方が好きだったから。
ベンチとってみた
えい。
#!/usr/bin/env perl use strict; use warnings; use Benchmark qw/cmpthese timethese/; my $COUNT = 100_000; my @array = (1,2,3,4,5); cmpthese timethese $COUNT, { contain_anonymous_ref => sub { [@array]; }, get_ref => sub { \@array; }, };
実行結果
:!perl bench/arrayref.pl Benchmark: timing 100000 iterations of contain_anonymous_ref, get_ref... contain_anonymous_ref: 0 wallclock secs ( 0.04 usr + 0.00 sys = 0.04 CPU) @ 2500000.00/s (n=100000) (warning: too few iterations for a reliable count) get_ref: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 10000000.00/s (n=100000) (warning: too few iterations for a reliable count) Rate contain_anonymous_ref get_ref contain_anonymous_ref 2500000/s -- -75% get_ref 10000000/s 300% --
結果
¥@array で取得したほうがいいなあ