Python 2.7 or Python 3 (for speed)?

Question:

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?

Asked By: MyNameIsKhan

||

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:

Answered By: Michael0x2a

Python 3.3 included an optimization for Unicode strings that reduced their memory consumption. That might translate into faster code if more of it fits into cache.

Really the only way to know is to benchmark your most critical code in both and see what the difference is.

Answered By: Mark Ransom

The necessity of Libraries for your applications will determine whether Python3 or Pyhton2 is better.

Answered By: dilip kumbham

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

Answered By: Óscar López
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.