開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9) の9章(リファレンスを使った実践的なテクニック), 9.9(練習問題)2を解いてみる。
その他参考書籍
2.
コード(TextWrangler)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
use Benchmark qw(timethese cmpthese) ;
my $sort = q{ my @sort = sort {-s $a <=> -s $b } glob "/opt/local/bin/*";};
my $schwartz = q{my @sorted =
map $_->[0],
sort {$a->[1] <=> $b->[1]}
map [$_, -s $_],
glob "/opt/local/bin/*"};
my $r = timethese(-5, {
'sort' => $sort,
'schwartz' => $schwartz,
});
cmpthese($r);
入出力結果(Terminal)
$ ./sample.pl
Benchmark: running schwartz, sort for at least 5 CPU seconds...
schwartz: 6 wallclock secs ( 2.52 usr + 3.46 sys = 5.98 CPU) @ 66.22/s (n=396)
sort: 5 wallclock secs ( 0.76 usr + 4.32 sys = 5.08 CPU) @ 15.16/s (n=77)
Rate sort schwartz
sort 15.2/s -- -77%
schwartz 66.2/s 337% --
$
0 コメント:
コメントを投稿