開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
その他参考書籍
2.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use Benchmark qw(:all);
cmpthese( -5, {
"変換" => q{
my @sorted =
map $_->[0],
sort {$a->[1] <=> $b->[1] }
map [$_, -s $_],
glob "/bin/*";
},
"標準" => q{
my @sorted = sort { -s $a <=> -s $b } glob "/bin/*";
},
});
timethese( -5, {
"変換" => q{
my @sorted =
map $_->[0],
sort {$a->[1] <=> $b->[1] }
map [$_, -s $_],
glob "/bin/*";
},
"標準" => q{
my @sorted = sort { -s $a <=> -s $b } glob "/bin/*";
},
});
入出力結果(Terminal)
$ ./sample.pl
Rate 標準 変換
標準 806/s -- -62%
変換 2143/s 166% --
Benchmark: running 変換, 標準 for at least 5 CPU seconds...
変換: 6 wallclock secs ( 2.18 usr + 3.06 sys = 5.24 CPU) @ 2146.18/s (n=11246)
標準: 5 wallclock secs ( 0.90 usr + 4.43 sys = 5.33 CPU) @ 802.25/s (n=4276)
$
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
import glob, os, time
# timeitモジュールの使い方がまだいまいち理解できなかったので手動で行う事に
n = 10000
files = glob.glob("/bin/*")
start = time.time()
for x in range(n):
files = map(lambda x: x[0],
sorted( map(lambda x: [x, os.path.getsize(x)], files),
key=lambda x:x[1]))
else:
stop = time.time()
print("変換: {0}".format(stop - start))
start = time.time()
for x in range(n):
files = sorted(files, key=lambda x: os.path.getsize(x))
else:
stop = time.time()
print("標準: {0}".format(stop - start))
入出力結果(Terminal)
$ ./sample.py 変換: 4.281044960021973 標準: 3.8019859790802 $
変換した方が遅くなった。><
0 コメント:
コメントを投稿