Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 9, 2021 05:20 pm GMT

What makes languages "fast" or "slow"?

In his article Naser Tamimi compares C++ and Python and this is what he observed:
image

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

  • 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

  • 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

Statically-typed Language

  • A statically typed language is one in which variable types are known at the time of compilation. The data types of thevariables must be specified in statically-typed languages, and once defined, the variable type cannot be modified. eg: C, C++, Rust, Java
/* C++/C code */int number_1 = 1; //integernumber_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

  • 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

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 programcrashes.
image

  • 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

How does this matter?

Look at this python example:

def fact(n):    if n==0:        return n    return n*fact(n-1)

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.


Original Link: https://dev.to/vibalijoshi/what-makes-languages-fast-or-slow-3l44

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To