In his article Naser Tamimi compares C++ and Python and this is what he observed:
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
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.
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
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
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.
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 program crashes.
- 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.
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
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.
Top comments (14)
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.)
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.
I need to try out Go soon 🔥🔥
Ahh I love the style of your writing! Very well explained in brief words ( better than I could tbh!)
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 ofgc.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.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 🙌🏻
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 :]
Agreed, there's a lot of stuff to care about when choosing a language
How many programmers are using this language ? Companies should care about this because it's easier to find programmers to hire in a popular language it's also more likely to find a answer to a question you may have in stackoverflow or any other place, because the language gets used a lot more so there's a bigger chance someone had the same problem as you
How fast are developers getting results on this language ? I think libraries,frameworks and tools are more important here than the language itself, the whole idea of "getting results" for me at least is more linked to a combination of what tools,frameworks and libraries you're using, you're likely to struggle if you choose the "wrong tool" for the job
Is my application "that" time critical ? this is a good thing to ask yourself but i think that some companies start with small apps that get bigger over time, sometimes the choice of a algorithm or way to do something is more important than the speed itself, for example a CPU renderer in C could be slower than GPU renderer using python or javascript
sometimes even "higher level languages" implement libraries as just a wrapper over lower level languages (as it happens a lot in python for example) so it may be wise to use a high level language and write some specific time critical pieces of your code in a shared library in a lower level language if you don't find a good library for the job
Let's have that nerd conversation sometime 😎
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.
Loved the diagrams. Nice job explaining!
Thank you so much for your appreciation 🦋
Excuse me
Double check your factorial code
It will always returns zero 😊
Ohh yes! Thank you so much for pointing out 🙌🏻
Some comments have been hidden by the post's author - find out more