xrange? OS? なんのちがいだろ?
pythonのコードを多少高速化してみたみたいなエントリーを見つけて自分もやってみたけど、結果がえらい違った。
上記Blogでテストされている方は、i7のメモリ6GBとか結構ゴージャスな環境でやっているみたい、私はMBP15インチ上でOSX付属のPythonでやってみた。
atusiMBP15% python Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import profile >>> profile.run('sum(xrange(1,10000001))') 4 function calls in 0.144 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.007 0.007 0.007 0.007 :0(setprofile) 1 0.136 0.136 0.136 0.136 :0(sum) 1 0.000 0.000 0.136 0.136 <string>:1(<module>) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 0.144 0.144 profile:0(sum(xrange(1,10000001))) >>>
一番軽いパターンで試してみたけど、0.144 CPU secondsで終わってる。上記エントリーやつだと0.811 CPU secondsかかってる。
5倍以上差があるんだけど、全く同じコードで、よりしょぼい環境で5倍速いってなんでだろうなぁって思った。
Ubuntuとかだったらどうなるんだろう・・・sakuraVPSのubuntuで試そうとしたけどprofileが無いって怒られた。