DEV Community

Cover image for What makes languages "fast" or "slow"?
vibalijoshi
vibalijoshi

Posted on • Updated on

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 the variables 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; //integer
number_1 = "digit"; //ERROR! cannot be changed to string
Enter fullscreen mode Exit fullscreen mode
  • 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. 

Enter fullscreen mode Exit fullscreen mode

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

  • 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:

# 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
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (14)

Collapse
eljayadobe profile image
Eljay-Adobe

Scripting style languages — like Python, Ruby, Lua, JavaScript — are fast to modify-and-run. They don't have the disadvantage of compile and link steps. (The engine will usually on-the-fly tokenize or bytecode the program, and then run that in the scripting engine.)

Compiled style languages — like C++, C, D, Rust — are fast to run because they are compiled to native code. But have a slower turn-the-crank because of the compile and link steps, and arguably are fussier languages which makes them slower to develop in as well.

Java and C# hit the sweet spot of combining all the disadvantages of a scripting style language, with all the disadvantages of a compiled style language. (Kidding! Poking the bear. I like both Java and JVM, and C# and .NET.)

Collapse
ancienthello profile image
AncientHello

This is why I use Go.

It compiles lightning fast. It runs lightning fast.

I've never had fuss around to get someones project to compile. It just works. And it is quick.

The best of both worlds.

Collapse
vibalijoshi profile image
vibalijoshi Author

I need to try out Go soon 🔥🔥

Collapse
vibalijoshi profile image
vibalijoshi Author

Ahh I love the style of your writing! Very well explained in brief words ( better than I could tbh!)

Collapse
markclewis profile image
Mark Lewis

In general I appreciate this article for trying to explain something complex in simple terms, though there are many aspects I think it over-simplifies. One of the big ones is that a lot of the performance on modern computers depends on memory access and this article doesn't mention that at all.

The reason I'm commenting though is because of the following quote. "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." In my experience, this is outright false. The lack of a compiler might let you start debugging faster, but it doesn't make finding and fixing bugs faster. Indeed, I would argue that it normally makes debugging much slower. An example that I saw from one of my students last year was that in JavaScript he had typed gc.fillstyle=... instead of gc.fillStyle=.... He spent hours looking for this error and didn't find it before he came to me for help. In a statically typed language that typo would have been found in seconds with a modern IDE or at worst minutes by the compiler. Thanks to the joys of dynamically typed languages though he spent hours on it. This isn't just an issue for students. My own experience as a professional developer has led me to hate dynamic languages for anything more than a few lines of code because of these issues. Typos that should be found by tools immediately become serious debugging sessions in JavaScript, Python, or other dynamically typed languages.

Collapse
vibalijoshi profile image
vibalijoshi Author • Edited on

Thank you so much Mark for putting this forward! There is just so much going behind the scenes for every language that it made it difficult for me to pin down on a common conclusion.

I totally agree with your point that this article does not cover every aspect. When I was researching about this, there was no place where I could practically understand it. So I just took and initiative to make it simple so that beginners like me would get an idea.

Thanks to experienced people like you who can provide us with knowledge that even the internet can't. I'll research on the memory aspect and improve my article. Thank you so much once again 🙌🏻

Collapse
thomasjunkos profile image
Thomas Junkツ • Edited on

This kind of discussion is very academic. When choosing a language other metrics are more important than how fast the language is. E.g. the first questions should be: How many programmers are out there using the language? How fast are the developers producing results in the chosen language? Is my application that time critical? What about: How is your data organized? How is the ecosystem of the product: Databases, caches etc. (which would compensate most of the "slowness" of the language?

These are the questions focusing on your product.

Whether a language is fast or not is from my POV a nice question for sitting in a pub with a nice drink at hand and having nerd conversations :]

Collapse
vibalijoshi profile image
vibalijoshi Author

Let's have that nerd conversation sometime 😎

Collapse
andrewpierno profile image
Andrew Pierno

You made it seem so easy. I like the illustrations, too!
Would you be willing to write some tutorials for our us? Happy to pay.
You can either DM me on twitter at twitter.com/AndrewPierno or fill out this little airtable form airtable.com/shrN6S3NMZ7oxRXTt.

Collapse
aboss123 profile image
Ashish Bailkeri

Loved the diagrams. Nice job explaining!

Collapse
vibalijoshi profile image
vibalijoshi Author

Thank you so much for your appreciation 🦋

Collapse
balighmehrez profile image
Info Comment hidden by post author - thread only accessible via permalink
BalighMehrez

Excuse me
Double check your factorial code
It will always returns zero 😊

Collapse
vibalijoshi profile image
vibalijoshi Author • Edited on

Ohh yes! Thank you so much for pointing out 🙌🏻

Some comments have been hidden by the post's author - find out more