実行速度計測の記事に対して、MacbookProでやったほうが速かったとの記事がでていたので、原因を考えてみました。
- Pythonのバージョンの違い
このブログでの評価はPython2.5系、処理が速く終わったのはPython2.6系 - OSの違い(MacかWindowsか)
このブログでの評価はWindows7、速かったのは環境はMacOSX 10.6.6 - 演算ビット数の違い
このブログでの評価は64ビットCPUを使っているが演算は32ビット、MacOSX上では64ビットで処理されたかも
Pythonバージョンの違い
諸都合により、今回の計測は「WindowsXP on Core2Duo 3.06GHz(iMac 27′)」で行なっております。
Python2.5
1.14秒かかりました。
1 2 3 4 5 6 7 8 9 10 11 | >>> profile.run('sum(xrange(1,10000001))') 4 function calls in 1.140 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 1.140 1.140 1.140 1.140 :0(sum) 1 0.000 0.000 1.140 1.140 <string>:1(<module>) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 1.140 1.140 profile:0(sum(xrange(1,10000001))) |
Python2.6
0.918秒かかりました。2割程度速くなっているようです。
1 2 3 4 5 6 7 8 9 10 11 | >>> profile.run('sum(xrange(1,10000001))') 4 function calls in 0.918 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.918 0.918 0.918 0.918 :0(sum) 1 0.000 0.000 0.918 0.918 <string>:1(<module>) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 0.918 0.918 profile:0(sum(xrange(1,10000001))) |
演算ビット数の違い
自宅の64ビット環境で測定してみました。Corei7 875Xです。OCはしていません。0.834秒でした。64ビットのPythonがインストールされているにもかかわらず、何故かsys.maxintの値は32ビット版の値になっており、長整数型に変換されてしまいました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Python 2.5 (r25:51908, Sep 19 2006, 10:05:36) [MSC v.1400 64 bit (AMD64)] on win 32 >>> import profile >>> profile.run("sum(xrange(1,10000001))") 4 function calls in 0.834 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.834 0.834 0.834 0.834 :0(sum) 1 0.000 0.000 0.834 0.834 <string>:1(<module>) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 0.834 0.834 profile:0(sum(xrange(1,10000001))) >>> import sys >>> sys.maxint 2147483647 |
Python2.6
0.771秒です。やはりPython2.6のほうが早いようです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32 >>> import profile >>> profile.run("sum(xrange(1,10000001))") 4 function calls in 0.771 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.771 0.771 0.771 0.771 :0(sum) 1 0.000 0.000 0.771 0.771 <string>:1(<module>) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 0.771 0.771 profile:0(sum(xrange(1,10000001))) >>> import sys >>> sys.maxint 2147483647 |
まとめ
Python2.6はPython2.5より、1~2割程度高速である。長整数型に変換されているのが処理速度の低下の原因だと思うが、64bit版のPythonを使っているにもかかわらず、何故か64ビット演算されない。WindowsのPythonで、64ビットの整数型が使えないはずはないと思うので、どうやればよいか調べる。なお、「GAE/Python の実行環境を探る」によると、GAE/Pythonでは、整数型は64ビットで扱われているようです。
【追記】
Windows上だと、64ビットPythonでもIntが32ビットになるのは仕様だそうです。
sys.maxint in Python 2.6.1 (amd64) on Windows XP x64
結論
Windows上では、64bit版のPythonを使っていたとしても、int型は32ビットで扱われる。性能測定に使った処理は、32bitには収まらないが、64ビットには収まる値を扱っていたため、Windows上では長整数型、Mac上ではint型での計算となった。長整数型の計算は、int型の計算と比べてコストが大幅に大きいため、Windows環境では大幅に処理時間が伸びてしまった。Pythonのバージョンによる差異もあるが、せいぜい1-2割の差である。よって、演算ビット数の違いが大きな性能につながったのだといえる。