Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 or Python 3 (for speed)? [closed]

Tags:

python

I've looked around for answers and much seems to be old or outdated. Has Python 3 been updated yet so that it's decently faster than Python 2.7 or am I still better off sticking with my workable code?

like image 871
MyNameIsKhan Avatar asked Jun 05 '12 16:06

MyNameIsKhan


People also ask

Which is faster Python 2 or 3?

Python 3.3 comes faster than Python 2.7.

How much faster is python3 than python2?

Python 3.7 is 1.19x faster than Python 2.7, but the only Python 3. x release to beat the Python 2.7 benchmark I ran. The speed.python.org benchmark shows similar results. PyPy smashes any of the CPython results, but with PyPy3 twice as slow as PyPy.

Is Python 3 slower than python2?

maxint was dropped from Python 3, but the integer value is basically the same. The speed difference in Python 2 is thus limited to the first (2 ** 63) - 1 integers on 64-bit, (2 ** 31) - 1 integers on 32 bit systems.

Which Python version is the fastest?

In this small synthetic benchmark, PyPy is roughly 94 times as fast as Python! For more serious benchmarks, you can take a look at the PyPy Speed Center, where the developers run nightly benchmarks with different executables.


2 Answers

The issue isn't about speed -- they're either the same speed or Python 3.x is faster (depending on which benchmarks you look at). More specifically, Python 2 used to be faster, but apparently, they're on par with each other now (?). See the comments and this slide deck (towards the back).

The core devs are also actively working on optimizing Python 3 -- each new release of Python 3 has been faster than the last. You can keep abreast of the latest proposals and ideas on optimizing Python (as well as the many complications) by monitoring the python-dev mailing list.

Rather, the reason many people used to give for not updated is because when Python 3 came out, and for several years after, the majority of Python libraries were not updated to work on Python 3.x. However, thankfully, this situation is vastly improved today -- the majority of 3rd party libraries are now Python 3 compatible.

The Python FAQ contains more info. You can also check the Python Wall of Superpowers or Py3 Readiness to get an overview of which popular Python libraries are currently compatible with Python 3.

Update: (Summer 2017)

I feel somewhat obligated to note that support for Python 2 is formally ending in 2020.

Many 3rd party libraries are also planning on following suit -- a large subset of the scientific ecosystem (e.g. matplotlib, pandas, ipython) are planning on dropping support for Python 2 in 2020, Django is dropping support... I wouldn't be surprised if other libraries do the same.

So, if you're interested in making sure you can use the latest and greatest features in either Python or your favorite library, you should look into migrating to Python 3 sooner rather than later.

Here are some guides on converting Python 2 to 3 code:

  • https://docs.python.org/3/howto/pyporting.html
  • http://python3porting.com/
  • https://eev.ee/blog/2016/07/31/python-faq-how-do-i-port-to-python-3/
like image 88
Michael0x2a Avatar answered Oct 16 '22 02:10

Michael0x2a


FYI, here's a performance benchmark comparing Python 2.7 with Python 3.3. Python 3.3 comes faster than Python 2.7.

like image 31
Óscar López Avatar answered Oct 16 '22 03:10

Óscar López