開発環境
- 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.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; use Benchmark qw(timethese) ; my @list = glob "/bin/*"; my $count = -5; timethese($count, { '普通の変換 ' => 'sort { -s $a <=> -s $b} @list', 'シュワルツ変換' => 'map $_->[0], sort { $a->[1] <=> $b->[1] } map [$_, -s $_], @list;', });
入出力結果(Terminal)
$ perl sample.pl Benchmark: running シュワルツ変換, 普通の変換 for at least 5 CPU seconds... シュワルツ変換: 4 wallclock secs ( 5.13 usr + 0.01 sys = 5.14 CPU) @ 5589859.92/s (n=28731880) 普通の変換 : 3 wallclock secs ( 5.04 usr + 0.02 sys = 5.06 CPU) @ 14013822.53/s (n=70909942) $
0 コメント:
コメントを投稿