2008年7月26日土曜日

python code reading宿題その1

python code reading の宿題

methodを実行する場合とmethodをローカル変数に代入してから実行する
場合の速度の違い
下記コードと実行結果。

結論:
100000回実行したところCPUtimeが110sec , 109secと1秒しか変わらない。
試行回数を増やしてみれば変わるかもしれないが、ほとんど変わらない様子。

////////////////////////////////////////////////////////
#!/usr/bin/python
from StringIO import StringIO
import profile

call_cnt=1000000
def callfunc():
strIO=StringIO()
write=strIO.write
for i in xrange(1,call_cnt):
write(str(i))

profile.run('callfunc()')

実行結果:
5000002 function calls in 109.890 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
999999 9.480 0.000 9.480 0.000 :0(append)
1000000 9.250 0.000 9.250 0.000 :0(isinstance)
1000000 8.040 0.000 8.040 0.000 :0(len)
1 0.010 0.010 0.010 0.010 :0(setprofile)
1 0.250 0.250 109.880 109.880 :1(?)
999999 52.030 0.000 85.690 0.000 StringIO.py:209(write)
999999 6.890 0.000 6.890 0.000 StringIO.py:38(_complain_ifclosed)
1 0.000 0.000 0.000 0.000 StringIO.py:54(__init__)
1 0.000 0.000 109.890 109.890 profile:0(callfunc())
0 0.000 0.000 profile:0(profiler)
1 23.940 23.940 109.630 109.630 sample_write.py:7(callfunc)



//////////////////////////////////////////////////////////////

#!/usr/bin/python
from StringIO import StringIO
import profile

call_cnt=1000000
def callmethod():
strIO=StringIO()
for i in xrange(1,call_cnt):
strIO.write( str(i))

profile.run('callmethod()')


実行結果:
5000002 function calls in 110.210 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
999999 7.710 0.000 7.710 0.000 :0(append)
1000000 9.580 0.000 9.580 0.000 :0(isinstance)
1000000 7.600 0.000 7.600 0.000 :0(len)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.260 0.260 110.210 110.210 :1(?)
999999 50.450 0.000 82.330 0.000 StringIO.py:209(write)
999999 6.990 0.000 6.990 0.000 StringIO.py:38(_complain_ifclosed)
1 0.000 0.000 0.000 0.000 StringIO.py:54(__init__)
1 0.000 0.000 110.210 110.210 profile:0(callmethod())
0 0.000 0.000 profile:0(profiler)
1 27.620 27.620 109.950 109.950 sample_strio_write.py:6(callmethod)

0 件のコメント:

コメントを投稿