T
T
TheSnegok2021-10-13 16:50:14
Programming languages
TheSnegok, 2021-10-13 16:50:14

Why are the speeds of different languages ​​different?

Why do all languages ​​have different speed if they translate everything into binary code? Even if we take, for example, two almost identical lines of code from different languages, will they still be different in binary?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
V
Vladimir Kuts, 2021-10-13
@TheSnegok

Let's say we need to add two numbers - 2 and 3
In assembler:
Put 2 in the ax register, 3 - in the bx register, execute the register addition command - as a result, we have the answer in ax.
Roughly speaking, we have executed only 3 commands at the processor level.
In C:
Stored register values, made initializations, performed 3 previous addition commands - restored register values. We have already spent at least a few commands more on registers / initialization.
Some high-level language with dynamic typing:
Take a variable and convert it to a number. They took the second variable - converted it into a number. They called a certain class that can add integers, floating point numbers, concatenate strings, etc. Passed these two of our variables. In addition to the actual addition of numbers, explicit and implicit conversions are performed, checks for exceptions are performed, class methods that can do many operations are called, and so on, initial initializations, on-the-fly optimizations, register saving, etc. are performed ...
That is, for the task of folding numbers, an order of magnitude more processor instructions are already used.

V
Vasily Bannikov, 2021-10-13
@vabka

same lines of code from different languages

Even more I will say - two identical lines in the same language, but compiled (or run) by different compilers (or different runtimes) or the same compiler (or runtime) with different different options.
Will they still be different in binary?

Yeah. Because compilers and interpreters are implemented differently everywhere.
+ In all languages, the standard library is of different sizes and it can also be loaded at different speeds.
Although in very primitive cases there may be identical code.

R
Roman Mirilaczvili, 2021-10-13
@2ord

  1. compilers produce optimizations of different quality and capabilities, as a result of which there can be very different target machine code for the processor instruction set. Some compilers create a lot of overhead and less compact code.
  2. Languages ​​can have different runtimes (also compiled to native code), which also affects execution speed. Some languages ​​compile to a generic bytecode that is independent of the processor. You also have to pay a price for this.

P
pfg21, 2021-10-13
@pfg21

curvature and flaws in code optimization.
or vice versa, the troubles of the layers of protection of iron from the user / pogromist.

I
ikutin666, 2021-10-13
@ikutin666

lines can be generally identical 1 to 1, but in different languages ​​the compiler can process them differently, therefore, the machine code will be different

I
Ighor July, 2021-10-13
@IGHOR

Binary code is different. There is assembler code, it is also binary, and there is the so-called "byte code" - created for some kind of runtime runtime environment.
Assembly code is always faster than any runtime execution.
You can clearly see it here https://godbolt.org

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question