Β
π€ Ever wondered why different programming languages have different performance levels? Let’s dive into this fascinating topic together! π
programming #languages #performance #quantitativeanalysis
So, why exactly do different programming languages perform differently? Here are some key points to consider:
- Underlying architecture: Each programming language is built on a different architecture, which can impact its performance.
- Compilation vs. Interpretation: Languages that are compiled tend to have better performance compared to those that are interpreted.
- Optimization techniques: Some languages have built-in optimization techniques that can improve performance.
- Resource management: How a language manages resources such as memory can also affect its performance.
Do you have any materials or insights to share on this topic? Let’s discuss and learn from each other! Together, we can gain a deeper understanding of why programming languages vary in performance. π‘
Here
https://en.wikipedia.org/wiki/High-level_programming_language
https://en.wikipedia.org/wiki/Programming_language_design_and_implementation
> Why different programming languages have different performance
They don’t. Different programs have different performance.
There are three major factors.
Number one is, there are compiled and interpreted languages. Before executing a program, the code needs to be translated into a set of machine instructions that can be fed to the processor. In interpreted languages, e.g Lua or Bash, this just happens line by line as the code executes. The interpreter reads a line from user input or from a file, interprets it (duh), and decides what to do, then proceeds to read the next line. In compiled languages, the program as a whole is converted into an executable. This allows the compiler to apply optimizations. A couple of examples: a) Inlining: youΒ extract some code into a function for better readability, but you only call that function with 10 different sets of arguments. The compiler may decide to inline the function – replace function calls in your code with actual function body, removing the extra machine instructions associated with making function calls. b) Branch elimination: you are using a function from a library, and there is an if/else statement somewhere in that function body. But, the way your code uses that library, only the βelseβ branch is actually ever executed. The compiler may notice it and remove the if / else entirely, eliminating the unnecessary check. Compiled languages may also have varying amount of optimizations, which can make them faster or slower. There are also hybrid approaches such as βjust in time compilationβ, used for example in JS, which allows an interpreted language to have some performance advantages of the compiled ones.
Number two is, memory management. There are languages with manual (C, C++) and automatic (Go, Java) memory management. With manual management, the programmer needs to allocate memory to store variable values, and deallocate it when the value no longer needs to be used. This adds complexity to the language and leaves space for human errors, such as use-after-free and memory leaks (forgetting to free the memory). For this reason, many languages choose garbage collection, in which a special automatic algorithm regularly checks for variables that are no longer accessible (e.g. gone out of scope), and frees their memory. Because the algorithm doesnβt know the semantics of your program, this is slower than manual management, and requires the program to periodically halt to execute the algorithm (GC pause).
Number three is, some languages are designed to only work with higher level abstractions. They hide certain details from the developer and reduce the amount of decisions that need to be made, at the cost of worse performance. For example, in high-level Lua, associative key-value arrays are always represented as tables, which are hashmaps under the hood. In lower-level Rust, you can choose between structs and arrays, that are extremely efficient but can only be used with a fixed set of keys, and many different types of map-like data sturctures (hashmaps, btreemaps, etc)
The reason is that the language itself is not directly executed. Computers do not understand and execute the language we write directly. Additionally, each programming language uses different methods to create an environment that the computer can execute. This is why the performance of each language varies.