最近、このスレッドで Jon Clements の助けを借りて、次のコードの実行時間が大きく異なることを発見しました。
なぜこれが起こっているのか分かりますか?
コメント: self.stream_data は多くのゼロと int16 値を持つベクトル タプルであり、create_ZS_data メソッドはいわゆる ZeroSuppression を実行しています。
環境
入力: 多くの (3.5k) 小さなファイル (それぞれ ~120kb)
OS: Linux64
Python ver 2.6.8
ジェネレーターに基づくソリューション:
def create_ZS_data(self):
self.ZS_data = ( [column, row, self.stream_data[column + row * self.rows ]]
for row, column in itertools.product(xrange(self.rows), xrange(self.columns))
if self.stream_data[column + row * self.rows ] )
プロファイラー情報:
ncalls tottime percall cumtime percall filename:lineno(function)
3257 1.117 0.000 71.598 0.022 decode_from_merlin.py:302(create_ZS_file)
463419 67.705 0.000 67.705 0.000 decode_from_merlin.py:86(<genexpr>)
ジョンのソリューション:
create_ZS_data(self):
self.ZS_data = list()
for rowno, cols in enumerate(self.stream_data[i:i+self.columns] for i in xrange(0, len(self.stream_data), self.columns)):
for colno, col in enumerate(cols):
# col == value, (rowno, colno) = index
if col:
self.ZS_data.append([colno, rowno, col])
プロファイラー情報:
ncalls tottime percall cumtime percall filename:lineno(function)
3257 18.616 0.006 19.919 0.006 decode_from_merlin.py:83(create_ZS_data)