What makes languages "fast" or "slow"? 
Tags: beginners, tutorial, programming, performance
Published: 2021-10-09 17:20:38


In his article [Naser Tamimi](https://towardsdatascience.com/how-fast-is-c-compared-to-python-978f18f474c7) compares C++ and Python and this is what he observed: 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4bj750cvfhs6mpp5got.png)

As you can see Python is much slower than C++, but in reality no programming language is fast or slow. The machine code generated by the programming language is either simple or complex. 

## All Languages have the same goal
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbm63msseo58mmzywle8.png)
 
 
-  The low level languages like the machine code (displayed in binary digits) runs very fast because no translation program is required for the CPU, but it very inconvenient to write. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8owrxg1x4nil3zpocrbj.png)
 
- High Level Languages (HLL) such as C++ and JavaScript provide a simple human language-like environment for programmers to communicate with computers.
- Programming languages may differ in their syntax, but the common goal of all of them is to generate Machine Code, which is the only language that a computer can understand.

**As a result, it all comes down to how the code is converted into machine code.** 

To understand this, we must first understand the differences between statically and dynamically typed programming languages.

## Statically and Dynamically Typed Languages
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqu25e128x9aueet3f14.png)
 

**Statically-typed Language**

- A statically typed language is one in which variable types are known at the time of compilation. The data types of the variables must be specified in statically-typed languages, and once defined, the variable type cannot be modified. eg: C, C++, Rust, Java

```cpp
/* C++/C code */
int number_1 = 1; //integer
number_1 = "digit"; //ERROR! cannot be changed to string
```
 
- At compile time, type verification is performed. As a result, these checks will catch things like missing functions, invalid type arguments, or a mismatch between a data value and the type of variable it's allocated to before the program runs.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ilc113kc2ruk1ury3bb.png)
 
- A programmer can't run code unless it passes these type checks, thus it pushes them to rectify bugs by throwing errors and warnings during the compilation process.
- A substantial number of errors are discovered early in the development process.

**Dynamically-typed languages**

Those where the interpreter assigns variables a type at runtime based on the variable's value at the time. Eg: JavaScript, Python
```js
var name;
name = 57;
name = "Vibali"; //this will not throw any error. 

```
There is no compilation phase in JavaScript. Instead, a browser interpreter examines the JavaScript code, understands each line, and then executes it.
If the type checker identifies an issue at this point, the developer will be notified and given the option to fix the code before the program crashes.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfn0fg4ssiou2xrutx72.png)
- You don't have to wait for the compiler to finish before testing your code modifications because there isn't a separate compilation process. This makes the debugging process much faster and easier.

> Related: 
> [Difference between Compiler and  Interpreter](https://www.geeksforgeeks.org/difference-between-compiler-and-interpreter/)

### How does this matter?
Look at this python example:

```python
# Python 3 program to find  
# factorial of given number

def factorial(n):

    if n < 0:

        return 0

    elif n == 0 or n == 1:

        return 1

    else:

        fact = 1

        while(n > 1):

            fact *= n

            n -= 1

        return fact
```

_What is the value of n? Is it a numerical value? Is it string? Is it a class you created previously?_

The interpreter has no way of knowing what kind of data it will get. At run-time, you have to do a lot more checking, which implies you're doing more implicit work for basic actions.
All of these lookups are _extremely_ difficult to complete quickly.

*As a result, the time it takes to generate machine code increases, making these languages appear slower.*

On the other hand the Statically Typed languages like C++ or Java results in compiled code that executes more quickly because when the compiler knows the specific data types that are in use, it can produce **optimized machine code (i.e. faster and/or using less memory)**.

### Which language should I choose?

- This isn't to say that languages like JavaScript aren't useful. It is entirely dependent on the use case. Each language focuses on a distinct aspect. Dynamic typing allows programmers to construct types and functionality based on run-time data, but it comes at the sacrifice of speed. 

- Programmers can fine-tune their code to operate effectively in any context, even when there is little hardware space or energy available to power the application, by using C, C++, or Rust when building an operating system driver or file system.

So it all depends on the task that we want to achieve.

Let me know in the comments below which language you prefer and why.