String performance - Python 2.7 vs Python 3.4 under Windows 10 vs. Ubuntu

非 Y 不嫁゛ 提交于 2019-12-03 14:01:16

Although you are measuring speed of the same code, the structures in your code are different.

A. range in 2.7 is type 'list', range in 3.4 is class 'range'

B. 'ATG' * 10**6 in 2.7 is a bytes string and in 3.4 it's and unicode string

You can try to produce more compatible results if: a) use xrange for 2.7 variant, b) use bytes string in both examples: b'ATG' or unicode strings in both examples.

Update

I suspected that difference in performance stems from main factors: a) 32bit vs 64bit, b) C compiler.

So, I did tests for:

  1. ActiveState Python 2.7.10 32bit
  2. ActiveState Python 2.7.10 64bit
  3. Official distribution Python 2.7.11 32bit
  4. Official distribution Python 2.7.11 64bit
  5. Python 2.7.6 64bit on Ubuntu on Windows 10
  6. pypy-5.1.1-win32

What I expected

I expected that:

  • 64bit version will be slower
  • ActiveState will be a little faster
  • PyPy faster by magnitude
  • Ubuntu on Windows 10 - ???

Results

Test                    as32b   as64b   off32b   off64b  ubw64b  pypy5.1.1
Sliding, regular:       1.232   1.230   1.281    1.136   0.951   0.099  
Incremental, regular:   1.744   1.690   2.219    1.647   1.472   2.772
Sliding, byte string:   1.223   1.207   1.280    1.127   0.926   0.101
Incremental, bytes:     1.720   1.701   2.206    1.646   1.568   2.774
Sliding, xrange&bytes:  1.117   1.102   1.162    0.962   0.779   0.109
simple find in string:  3.443   3.412   4.607    3.300   2.487   0.289

And the winner on Windows 10 is .... Ubuntu Python compiled by GCC 4.8.2 for Linux!

This result was completely unexpected for me.

32 vs 64: turned irrelevant.

PyPy: as always megafast, except cases when it's not.

I can't interprete this results, OP question turned not so simple as it seemed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!