Why is there an apparent tension between the simplicity of the language and the execution time?

It seems to me that languages ​​that are pretty easy to use (like Python) often have slower runtimes than languages ​​that are considered more difficult to learn (like C ++ or Java). What for? I understand that part of the problem arises because Python is interpreted rather than compiled, but what prevents Python (or another high-level language) from compiling efficiently? Is there any programming language that you think does not have this compromise?

+4
source share
4 answers

Lets compare C and Python. By most accounts, C is more “difficult” to program than, say, Python. This is because Python automates a lot of work that C does not. For example, garbage collection is automated in Python, but the responsibility of the programmer lies with C.

The price of this automation is that these "high-level functions" must be generalized enough to "match" the needs of each program. For example, the Python garbage collector has a predefined graphics / garbage collection algorithm that may not be optimal for each application. On the other hand, C gives the programmer complete flexibility to define the graph and the GC algorithm as she wants it.

So, you have it, simplicity and performance.

+5
source

The problem of the effectiveness of high-level languages ​​(or at least dynamic languages) is related to the fact that it is usually not known what operations should be performed until the actual types of objects are resolved at runtime. As a result, these languages ​​are not compiled for simple machine code and must perform all the heavy lifting behind the covers.

+5
source

In principle, there is a main reason for all this: the number of instructions that need to be done for something.

In direct old C, most statements can be completed with just a few instructions, usually from just over 1 to about 4, and with an average value that can be close to 1.

In C ++, more happens on the covers, for example, calling a virtual method requires a few more instructions. Come on, this may be resolved by a good optimizer, but runtime type information may interfere with the optimizer.

In Java, you have the same problems, plus the code runs in an interpreter that extends each JVM instruction to one or more machine instructions - in some cases much more, especially if the code does not work long enough to take advantage of runtime options.

And Python has all these problems, but it needs to be analyzed at startup and has a flexible (more complex) type management system, which means a few more instructions.

Yes, Python can be compiled. There are even projects that try it . But hard compilation robs you of some of the benefits of dynamic languages.

+2
source

Python code execution time is slower than C ++, mainly because it is a dynamically typed language. See for example:

Why are languages ​​with dynamically typed slow

+1
source

All Articles