Ruby: test2.rb↓
def blk(arr)
arr.each {|v|
yield v;
}
end
# テスト用の配列の生成
a = [];
10000.times {|x|
a[x] = x;
}
blk(a){|x| puts x } # 1乗を出力
blk(a){|x| puts x*x } # 2乗を出力
blk(a){|x| puts x*x*x } # 3乗を出力
Perl: test2.pl↓
sub blk{
my $arr = shift;
my $func = shift;
foreach(@$arr){
&$func($_);
}
}
# テスト用の配列の生成
$a = [];
for(my $i=0;$i<10000;$i++){
$a->[$i] = $i;
}
blk($a,sub{print $_[0]."\n"}); # 1乗を出力
blk($a,sub{print $_[0]*$_[0]."\n"}); # 2乗を出力
blk($a,sub{print $_[0]*$_[0]*$_[0]."\n"}); # 3乗を出力
やはりPerlが速い。
$ time ./test2.rb > /dev/null
real 0m0.395s
user 0m0.298s
sys 0m0.017s
$ time ./test2.pl > /dev/null
real 0m0.186s
user 0m0.116s
sys 0m0.014s
【プログラマっぽい??の最新記事】

