r/AskComputerScience • u/Klinging-on • May 20 '19
If every language is eventually compiled into the most basic machine instructions then why do languages like C and C++ run faster than Java and Python?
I don’t understand why lower level languages run faster than higher level ones if they are all eventually compiled into machine code.
17
u/frikk May 20 '19 edited May 20 '19
Sepp2k has a great answer. I'll just add something as a simple example.
In C, let's say you want to set the 5th spot in an array to a new value. You kind of just do it. The compiler takes one or two cycles to find the spot, flash the new value, and you're done. In some cases it won't even check to see if you're even allowed to do this, or if the array exists at all. If you asked to set an area that's outside of the safe memory range that your array lives in, too bad. You just flashed the value somewhere else and probably corrupted your memory.
In python, this same operation takes hundreds of cycles because a single instruction in python is actually doing tons and tons of stuff on the backend. It's going to check the type of value that you're assigning. It's going to see if the array is long enough. It may even check for other inconsistencies, like validating your value against custom rules, or running hooks on your array object or runtime class. Even if there are no hooks, it may still check for them. Further, if you're taking an action that requires adding memory to the array (like an 'append'), python will take care of that too without you even knowing or caring.
On top of all that, most of the lower level stuff is actually written in C. So then you also have to translate all of these steps from the python runtime objects into the in-memory objects that are written in C. Think of that litle array as a tiny nugget of pure memory (written in C) but wrapped in a huge runtime onion with layers and layers of abstraction, protection, translation, validation, and everything else. It's still pretty dang fast, but that's tens of extra cycles that come along with each high level step. Those python objects have to be validated and checked as well. A single array may be dozens of lines of code behind the scenes (or even hundreds of lines when you include all the boilerplate code required to translate from python world to C world -- at least for CPython).
The point is that with a language like python, you're trading speed for convenience. For many things that's an acceptable trade-off and the main reason that you would pick a language like python. If something runs in 2 seconds instead of .02 seconds, that's totally fine.
5
May 20 '19
Excellent question. Here's my take on it
C++ and C can be architecture specific with compiler flags.
Java compiles to bytecode that runs on any JVM. JVM's take resources.
Java allocates nearly everything on the heap. That memory isn't slow, but asking for it is a syscall and has a slight hit on performance.
scripted languages that don't have static typing require overhead to remember what type everything is.
this is just conjecture, but compiler writers for c and c++ have more expectation to have high performance. Script interpreter makers dont try to make it slow, but they don't face as much criticism.
compiled programs start up and run immediately. Scripts and java have to fire up the interpreter/JVM which is a huge, but one time, penalty. So if you wrote grep or ls in java, it would be painfully slow because you had to pay that penalty many times. Whereas if you wrote a service in java, you'd never pay the penalty because it would always be online.
4
2
May 21 '19
The problem is that python and java ARENT directly compiled to machine code, and then ran. python does the conversion on the fly, and java compiles it into an intermediate language called byte code, which is then converted on the fly.
-2
u/zanidor May 20 '19 edited May 20 '19
The language doesn't determine how long it takes a program to run, the algorithm described with the language does.
Imagine you have a GPS device that gives instructions in English, and it generally gives you the fastest route. Imagine you have another GPS device that gives instructions in Chinese, and a passenger that is translating the instructions into English for you. However, maybe your translator isn't perfect, and occasionally tells you to turn left instead of right by accident. Whenever this happens, the GPS has to reroute, and it takes you longer to get places.
In both cases you are receiving directions in English, so why does it take longer to get there with the Chinese-to-English setup? It's not the target language that's causing the slowdown, it's imperfections in the translation that makes you take a longer overall route. Similarly, translating a higher level language to a lower level one can generate something slower than the optimal low-level program since the compiler may deviate from the optimal low-level computational path.
In the case of the compiler, it's not a pure translation "error", but rather limitations on what a compiler can do while guaranteeing the output program preserves the meaning of the original. Certain high-level languages allow the compiler to generate programs closer to their low-level optimum in more cases.
54
u/sepp2k May 20 '19
First of all, you should distinguish between a language and its implementation(s). How and whether code in a language is translated to machine code can and does vary widely between different implementations of a language. For example the Python implementation PyPy has a JIT compiler, but CPython does not.
This isn't true. Not every programming language implementation emits machine code (as mentioned above, CPython doesn't), nor does every language have at least one implementation that does.
Beyond that, it doesn't only matter whether machine code is generated - it also matters which machine code is generated (also when in the case of JITs). For example, a naive compiler for a dynamically typed language might generate type checks for every operations (while a more complex, optimizing compiler still wouldn't be able to avoid doing so in all cases) while a compiler for a completely statically typed language would have no reason to do so. Similarly virtual function calls will necessarily involve extra lookups that can't always be optimized away.
In addition, a language might use big integers as their default integer types, heavily feature reflection, or contain plenty of other features that tend to be costly to implement.
Even among implementations for the same language, performance can differ significantly, so it's not surprising that implementations for different languages with different features and requirements would have even more differences in performance.
And then there's of course the fact that some languages simply have more mature (and thus better optimized) implementations than others based on how long they've been around, how widely they're used, and how much performance is even a priority.