Why is Python slower than other languages
Why is Python slower than Java but faster than PHP?
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
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.
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:
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.
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.
- What are some beginner mistakes in chess
- How do I stop beating myself
- What was Obama's SAT score
- Use WordPress for your blog
- How can we advertise the Google business listing
- How fast is Malaysia developing in 2018
- Do children understand the importance of money
- Why aren't lawyers called doctors?
- Where to buy sage for cleaning
- Is there a perfect diet?
- How much do EC engineers earn
- How big is the earth in kilometers
- How does gravity depend on time?
- Why do Malay men like Chinese girls
- How many patent products are there
- Is It Healthy To Eat Burned Food?
- What started No Nut November
- What is a life lesson that you have learned
- Are women sometimes attracted to shy men?
- What are the different types of telemarketing
- Can you motivate me to work online
- Which country is easy to get jobs
- Is food a profitable business?
- What's wrong with the following resume