9

Modern Javascript implementations like V8 (Chrome), SpiderMonkey (Firefox), and Chakra (IE/Edge) all have JIT compilation, and a number of other optimizations to improve performance.

Why doesn't Python have these?

I have been looking at PyPy and IronPython, which both claim speed gains. PyPy I don't understand how a Python implementation written in Python, an interpreted language, will be faster than the reference implementation in C. IronPython, same idea but I don't see how the .NET Framework will increase speed.

  • 2
    IronPython - like any other .NET language - is compiled to Microsoft's "Common Intermediate Language" for which very mature JIT compiler implementations exist. – Doc Brown Apr 27 '17 at 6:04
  • 2
    A programming language is a specification (written in some document), not a software. Only programming language implementations are software (and can be compilers or interpreters). – Basile Starynkevitch Apr 27 '17 at 7:41
  • @BasileStarynkevitch: I don't get what you are trying to tell us with this comment. The OP is asking explicitly about CPython, PyPy and IronPython, which are specific Python implementations. – Doc Brown Apr 27 '17 at 10:43
  • @DocBrown Seems like a relevant point to bring up, given the last paragraph in the question, which shows a misunderstanding when it specifically calls Python an interpreted language. – 8bittree Apr 27 '17 at 15:44
19

Why doesn't Python have these?

I'm not sure why you think there are no Python implementations that care about performance. PyPy, IronPython, and Jython are all industrial-strength, production-ready Python implementations which care about performance. Pyston is an in-development implementation that was specifically created for performance. Unladen Swallow and Psyco were also projects to improve the performance of Python.

However, the fact that CPython users greatly outnumber the total combined user base of all the other implementations, that Unladen Swallow was rejected by the community, that most of these projects are either dead or struggling to attract developers should tell you something about how the Python community values performance.

This answer is a good example of the typical mentality of the Python community: instead of fixing the performance problems, they would simply prefer to write their code not in Python.

I have been looking at PyPy and IronPython, which both claim speed gains. PyPy I don't understand how a Python implementation written in Python, an interpreted language, will be faster than the reference implementation in C.

First off: it doesn't matter which language the compiler is written in. After all, the compiler is only executed once, so even if it were slow, that doesn't matter: the performance of the compiler is irrelevant, what is relevant is the performance of the output of the compiler.

Secondly, since it only matters how fast the output of the compiler is, and the compiler is written in Python, i.e. the language that it compiles, it can actually make itself fast by compiling itself.

Thirdly, there is no such thing as an "interpreted language". A language is a set of mathematical rules and restrictions. It is a specification. A piece of paper. A language isn't compiled or interpreted. A language just is. Compilation and interpretation are traits of a language implementation, more precisely, a compiler or interpreter (duh!), not the language. Every language can be implemented by a compiler. Every language can be implemented by an interpreter. You can mechanically generate a compiler from an interpreter and an interpreter from a compiler.

But all of this doesn't actually really matter, because PyPy is actually not written in Python. It is written in RPython. RPython consists of two parts, the RPython programming language and the RPython framework.

The RPython programming language is not Python. It is a different programming language. RPython is a statically-typed programming language, roughly on the same level on abstraction as Java, with roughly the same performance as C. RPython is a syntactic and semantic subset of Python, which means that every RPython program is a valid Python program and can be run by a Python implementation (albeit typically several orders of magnitude slower, but this is still useful for debugging because you get access to all of Python's tools, and interpretation starts right away, whereas compiling the language implementation typically takes about 5-10 minutes), but the converse is not true.

The RPython framework is a framework for writing high-performance dynamic language implementations in the RPython programming language. It includes a garbage collector, object space, meta-object protocol, pre-defined objects, types, and operations, and so on. But the crown jewel is its ability to automatically generate a JIT compiler from an interpreter: if you implement a language in the RPython framework, you only have to write an interpreter, the RPython framework takes care of the JIT.

There are plenty of language implementations on the RPython platform, not just PyPy.

IronPython, same idea but I don't see how the .NET Framework will increase speed.

Most implementations of the ISO CLI, such as the various .NET variants by Microsoft, or Mono, contain sophisticated garbage collectors, optimizers, and compilers. The same is true for Jython and Java implementations.

IronPython is a compiler, it compiles Python source code to DLR trees (DLR is the Dynamic Language Runtime), which are then further compiled to CIL byte code, which is then again typically further compiled to native machine code.

6

Modern Javascript implementations like V8 (Chrome), SpiderMonkey (Firefox), and Chakra (IE/Edge) all have JIT compilation, and a number of other optimizations to improve performance.

Why doesn't Python have these?

JavaScript is included in web browsers and significant amount of software today is designed to run in web browsers. This makes JavaScript performance very important making companies like Google, Apple and Microsoft invest a lot of resources in making JavaScript runtimes fast. Had this stream of money been redirected to Python, it would be equally fast.

PyPy I don't understand how a Python implementation written in Python, an interpreted language, will be faster than the reference implementation in C.

The idea is that once code is JIT-ed, it's no longer "interpreted". PyPI works by transforming Python code to machine code (e.g. x86_64 machine code) that is then directly executed on processor.

  • 3
    Especially relevant is that, in the web browser, there is no alternative to JavaScript (or at least, nothing generally available on all the major browsers yet). If you want fast performance in the web browser, then you have to have a fast JavaScript implementation. Whereas on platforms that run Python, you can almost certainly move to a another language that may have a better performing implementation. – 8bittree Apr 27 '17 at 15:54
-3

If you structure your python code into modules with a simple top level start-up as the main python file, (with very little code in it), then python compiles all of the rest of your code into machine independent byte code, these are the .pyc files that you see in your directory structure. This minimises loading and interpretation times after the first run.

If you start up your script with the -O or -OO flags or set PYTHONOPTIMIZE to a value greater than 0 then .pyo files are generated which are further optimised.

If you need high optimisation for some specific function(s) you can write them in C, C++, FORTRAN or GO and then use them from within python.

  • How do you integrate non-C extensions into Python? I am only aware of C-extensions for Cython. – user209881 Jul 9 '17 at 12:08
  • 1
    @Bey: Basically anything in a shared library, (.dll or .so), can be called from python provided you know the function signatures - these can be configured manually,generated by tools such as swing or even generated from doxygen documentation. stackoverflow.com/questions/5811949/… is helpful as is blog.heroku.com/see_python_see_python_go_go_python_go – Steve Barnes Jul 10 '17 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.