Why is Python slower than other languages

Why is Python slower than Java but faster than PHP?


From today's perspective, this question does not fit our Q&A format. We expect the answers to be supported by fact, reference, or expertise, but this question is likely to spark debate, argument, vote, or expanded discussion. If you think this question can be improved and possibly reopened, get help.
Closed 7 years ago.

I've seen different benchmarks many times that show how many languages ​​do a given job.

These benchmarks always show that Python is slower than Java and faster than PHP, and I wonder why that is.

  • Java, Python and PHP run in a virtual machine
  • All three languages ​​convert their programs into their custom bytecodes, the on the operating system
  • Both Java and Python can be "compiled" (for Python), but the module for Python will not be compiled

Python and PHP are written dynamically and Java statically. This is why Java is faster. If so, please explain how this affects the speed.

And even if the dynamic-vs-static argument is correct, it doesn't explain why PHP is slower than Python - because both are dynamic languages.

You can see some benchmarks here and here and here





Reply:


JVM code can be efficiently JIT compiled using a simple (and fast) ad hoc compiler. However, doing the same would be extremely difficult for PHP and Python due to their dynamic notation. JVM translates into relatively simple, native code, similar to what a C ++ compiler would produce, but for the dynamic languages ​​you would have to generate dynamic dispatch for literally all basic operations and method calls. This dynamic dispatch is the primary bottleneck for all languages ​​of this type.

In some cases it is possible to eliminate dynamic dispatch (as well as the virtual calls in Java) by using a much more complicated JIT compiler for tracing. This approach is still in its infancy and doesn't do too much of an abstract interpretation, and such a compiler is likely to choke calls (which are very typical of dynamic languages).

As for the difference between Python and PHP, the latter is of much lower quality. In theory, it could go faster, but it never will.







There is a common problem with this question that it is too absolute. It doesn't really make sense to say, "Language X is faster than Language Y". Computer language itself is not "fast" or "slow" because it simply expresses an algorithm. The real question should be on the order of "Why is implementing X1 of language X faster than implementing Y1 of language Y for this particular problem domain?"

Some speed differences will certainly fall out of the language itself, as certain languages ​​can implement certain domains more easily than others. But a lot of things that make implementation quick isn't the language. For example, you can't really say "Python is slower than Java" without considering whether it's CPython, IronPython, or PyPy. This is especially true for languages ​​that use a VM, as the speed is directly related to the quality of the VM.

Other than that, I'm working with a system that can't use JIT on our device for various reasons with a very popular JavaScript VM that it usually supports. This means that our JavaScript runs a lot slower than on a PC with a similar processor. That one change, not directly related to the language itself, means that JavaScript is "a few times slower than C ++" and "orders of magnitude slower than C ++" for the tasks that matter to us.

Also keep in mind that languages ​​differ in their performance characteristics in ways that are not directly comparable. Too many benchmarks only translate a program from language A to language B and do not take into account that the languages ​​differ in terms of their fast functions. (You can see this in any reasonable benchmark comparison, like the ones you refer to, as they often have notes like "Thanks to so-and-so for showing me how to do it in the Foo language.)"

Take this Java code for example:

It would be tempting to "rewrite" this in C ++ and compare the runtimes:

The thing is, any competent C ++ programmer will immediately recognize that this is not the fastest way to do anything in C ++. You can easily speed things up by adapting them to C ++:

The point is not that C ++ can be fast, but instead of writing benchmarks to compare languages, it's really, really difficult. To get it right, you need to be an expert in both languages ​​and write from scratch in both languages. Even then, you can easily reach areas where a language excels for a particular task. For example, I can write a version of Towers of Hanoi in C ++ that runs faster than Java on any sane compiler. I can do that by essentially cheating by using C ++ templates that were evaluated at compile time (http://forums.devshed.com/c-programming-42/c-towers-of-hanoi- using-templates-424148.html).

The point there isn't that I could say "C ++ is faster than Java" because my program immediately returned while the Java version ran for minutes (and hoping that no one noticed my program was half an hour needed to build.) The point is that for this vary narrow case, C ++ is faster. In other narrow cases it could be the other way around. So it's not "C ++ is faster" but "C ++ is faster in cases where you can use templates to evaluate the expression at build time." Less satisfying, but true.

Speed ​​differences in languages ​​mainly affect implementation. Compiled languages ​​are faster than interpreted languages. Compiling into native code will be faster than compiling into bytecode. This has far more implications than questions like whether the language is statically typed or not. And of course, good implementations will be faster than bad ones.

And don't forget that good programmers will produce code faster than bad programmers, often to an extent that clearly outweighs the language differences.


This is related to the quality of the compiler, the compiler of Java has been continuously optimized for much longer, and the optimization is more important because all code is compiled for Java. I'm not sure why Python is faster than PHP, but I'll bet that's because of Google's influence on Python.







Why Java is the fastest:

Statically entered + JIT compile + --server flag to aggressively recompile the running code.

Why Python is faster than PHP:

While Python is a dynamic language, it is still strongly typed. This means that the structures you have coded are capable of runtime optimization.

Why PHP Sucks:

It's basically Javascript on the server (no multithreading support, totally dynamic, loosely written).

The more the compiler knows about your code, the better it can optimize. Java is fully optimizable before and during execution. Python is hot-tunable and PHP is terrible. Facebook actually transpils their PHP to C before it gets onto the server.
https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/





The benchmarks are rather skewed in favor of extensive mathematical programming.

Unsurprisingly, Python is pretty good at complex math, considering where and why it was first written.

PHP, on the other hand, was developed to serve websites. It can do other things, but web pages are what it does best, and it is on par with or better than Java at this task.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.