yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Programming Language Speed Comparison


10m read
·Nov 3, 2024

Hey guys, this is Maads 101. Today, I'm going to be comparing the speed of seven different programming languages and sharing my results with you.

So, to do this, I've written three different programs in seven different languages pretty much identically, and I've run them timed, how long they take to run. I'm going to share the results and explain, you know, justify what I find. Hopefully, it'll give you a good idea of how fast different programming languages do different things.

I'm just going to go through all three programs and briefly explain what they do, and then review the times. If you want to see more, you can go to this GitHub repository; it has all the information. It has the list of languages that I used, and it has the list of ways that I run the languages because that's very important.

For instance, I run Swift using Xcode 6, prev3; for Python, I'm using Python 3 and Python 2; for JavaScript, I'm using Google's V8 engine. So it'll be important to look at this GitHub repository if you have critiques or anything like that.

I'm just going to go ahead and show you what I found.

So this first benchmark is essentially a really simple program. It just takes two command-line arguments. Here's the Python one, so you can see. You give it the size of an array, and you give it a number of iterations. It makes an array that's an array of integers starting with 0, 1, 2, 3, etc., going up to n minus one, where n is the size.

Then what it does is it just reverses that array a certain number of times that you specify. It just reverses it by swapping the first and last values, the second and second to last values, third and third to last values, etc. So you're just timing how long it takes to flip an array.

The thing is, it takes very little time to flip an array usually, so you want to maybe do it 10,000 times or something like that so that you can time it and get an accurate result that’s a couple of seconds. This way, there are not big biases or influence from your computer heating up or anything like that.

I'm going to go ahead now and show you the results. That was the source code in Python, just so you got an idea of what the program did. I was actually very surprised that Java was the fastest of all these benchmarks that I ran that just flipped an array.

I'm not sure why it was the fastest; maybe it did some kind of optimization for CPU cycles. Whatever it did, somehow it managed to make itself faster than C, even when I ran C with the highest level of optimization in Clang. I'm not sure how that happened, but somehow in this particular test, Java was faster than C.

Don't worry, in the other two, Java is not faster than C, so you don't have to attack me in the comments yet. But in this case, I just happened to find that Java was faster than C. If you want to download this and try it yourself, or see what's going on, go ahead. If you figure it out, please leave a comment.

I'm not entirely sure. You can see that C is actually listed here four times; that's just with the four different optimization levels. So -O0 is no optimization, -O1 is a bit, -O2 is even more, and -O3 is the most. In this case, these three didn't make too much of a difference. O3 was pretty close to O1, but when you really have no optimization, it makes it a lot slower.

So we have Java, C. Then we have Dart and JavaScript, and Dart and JavaScript are almost neck and neck. Dart is significantly, you know, moderately better.

Something to know about Dart and JavaScript is I'm using V8 to run JavaScript and I'm using the Dart VM to run Dart. These two engines were written by basically the same team at Google. So the reason why Dart is faster isn't that a better team wrote it or more money was put into it; it's actually just that it turns out that because Dart has static typing and more structure, it can be made faster and more optimized.

That is why JavaScript is a little slower. It's actually pretty impressive that JavaScript got this fast, because arrays in JavaScript are untyped and can have all this nonsense. But actually, unoptimized C is slower than both JavaScript and Dart. So if you just compiled this just in terminal with no optimization arguments or anything, you would actually find that the equivalent JavaScript program would be faster.

You tend to see this a lot in the real world, maybe not in these benchmarks, but this is pretty common that unoptimized is slower than JavaScript.

Here we have Swift, which is significantly slower than all the ones I've gone over so far. Now, this is fully optimized Swift with the highest level of optimization in Xcode, both from LLVM and Swift-specific optimizations. Why is it so slow? Maybe it's because Apple just changed the way arrays work in Swift, and they haven't optimized for it yet. I don't know, but you can certainly look into that too.

Then Ruby and both versions of Python are pretty much the same speed, and they are much slower. I think they're like 380 times slower than the fastest one, which is Java. So, if you're trying to reverse an array really fast, don't use Ruby or Python. So that's the lesson you should learn from this.

Now, let's talk about the next benchmark, which does a little bit less. It's not as useful. Essentially, what it does is I have one function that sums up all the numbers from 1 to n. So it does 1 + 2 + 3 + 4, etc.

Then it has another method that calls that method n times and basically adds up each result and then averages it with the last average. I call it a rolling average; it's not really a rolling average. I don't know what it is; it's not useful for sure.

But what it does do is force the program to call add up to n times and add up to does something n times. In the end, it ends up taking n squared time to run this program.

So this program is essentially just seeing how fast the programming language calls functions and how fast it does basic arithmetic. The results are going to be shocking; I'm warning you now. I will explain why they're so weird in a moment, but here let's go ahead and look at these.

So you can see right now that C took something like, well, oh, I mean this is a difference of probably about 10,000 between optimized C and Python, maybe more; maybe it's 100,000. Either way, I found that all these from C to Swift to Java were ridiculously fast compared to these other ones—disproportionately fast.

So Dart versus Java, like, they're the closest ones between this boundary, and it's a factor of 20. So the question is, how come these five languages were faster than these other ones?

The reason is, at least for C and Swift, I know for sure; I think Java did something very similar. What they did was they looked at this method that adds up all the numbers from 1 to n and changed it from adding each number to just n * (n + 1) / 2, which is a formula. If you haven't learned it, that's fine. That formula gives you the exact sum from 1 to n, and it's much faster than adding up all the numbers.

So that changes the whole thing. I think I said here from O(n^2) to just O(n), so it makes it way faster. You know, the bigger the number I put in, the faster it will be relative to the other one.

It's kind of an unfair comparison, but at the same time, it's a very fair comparison because it shows that the C optimizer, you know, Clang, and Clang also compiles Swift, so it got the same optimization.

How the Java optimizer actually was able to recognize something I was doing inefficiently—that's a pretty intelligent thing to recognize and make it faster. It's a very specific example, and it still did it, so I was highly impressed by this.

Now these other languages did not notice that, so Dart, without noticing that, ran a lot faster than JavaScript in this case, probably, once again, because Dart is statically typed. Unoptimized C, of course, didn't, because I didn't have the optimization on; it didn't use that formula, so it's even slower than JavaScript.

Like I said before, that tends to happen a lot. Then Ruby and Python are even slower, so Ruby is actually a lot faster than Python in this case. That tends to be the case; Ruby is faster than Python. Although in the last example, they were about the same speed because it was a simpler test, you can see the difference here is pretty big.

And Python 2 in this case is actually slower than Python 3. I have these in the wrong order, and I'll switch them after I make this video. But anyway, that's basically the results and that's an explanation of why these are so much faster, which is pretty cool.

It's cool that we have that kind of like artificial intelligence. But anyway, let's go ahead and look at the final benchmark.

So this one was a real pain to write; that's why you can see this commit message: "Just one more to go." Oh God, yeah. That's because I was suffering while writing these. What it is, is—I'll show you one that doesn't have all that compiled crap—here's the Ruby one.

It has a vector class, which has an X and a Y component, and you can add vectors and scale them and whatever. It has a particle class which has a location, a velocity, and mass.

Basically, this program uses these two classes to emulate two similarly charged particles just next to each other and emulate what happens as they get repelled apart. As they get repelled apart, the force diminishes. If you're familiar with physics, it's an inverse square relationship.

Anyway, this isn't the most efficient way to do any of this, but I did it exactly the same way in all of them. Basically, all of the programs involve creating vector objects all over the place, multiplying vectors by scalers, adding vectors, you know, physics stuff.

So it's stuff that would probably have been better done with OpenCL, and I probably could have gotten an even better result then, but this is what I got just using the processor.

As you can see, what happens is I did basically—it's an approximation—and I did 5 million samples. Between each sample, this amount of time is allowed to pass.

So let's have a look at the results. Here, we see that surprisingly, C with less optimization is faster than C++ in this case. That's just because these are probably the exact same program and there's a slight fluctuation because your computer's doing different things, or the OS schedules it differently.

I'm pretty sure that these were about the same; it just happened that these were the times I got, so I had to put them in there in the wrong order. But whatever! Java was faster than D1 or D0 C++, but it was slower than the fully optimized C++ by a decent amount.

That's probably because when I used C++, I used something called stack allocation, which is a lot faster than what I assume Java uses, which is heap allocation. It may be Java is smart enough to optimize it; in fact, it probably is. In order to get this time, it probably has to be.

It probably used some stack allocation too, or it didn't even use allocation at all. But that would explain why Java did a little worse than C++ in this case: because creating new objects in Java might be a little more expensive.

Now Swift, like fully optimized Swift, is still slower than non-optimized C++. You know, completely unoptimized. This is probably because Swift has an expensive object system with reference counting, and I might not have done it the best way in Swift, to be honest. But I think I did a pretty good job, so if you want to look at my Swift code, go right ahead.

Like I said, I think this repository will have a link in the description. Now JavaScript is actually like twice as slow as Swift, which, you know, is a little bit bothering, but whatever. JavaScript object creation, garbage collection is a little more expensive.

And Dart—I was surprised to see that Dart was actually slower than JavaScript in this case. That's pretty consistent with this particular object-oriented test, and it makes me believe that perhaps because Dart is still not fully developed, they haven't gotten around to optimizing the memory management the same way.

That's probably what it is; maybe I just did something stupid in Dart too. But I doubt it, because this is a significant difference.

Now, of course, we have the usual slow guys: Ruby. I mean, Python is really Python 3. I mean, let's look at compare this time to the optimized C. Like, how do you even make a programming language 465 times slower than C++? I mean, you have to work pretty hard to make it so bad. But whatever.

I'm sure lots of you like Python because of just these results, just these findings alone, I don't like Python that much anymore. I mean, look at how much— even in Ruby. I mean Ruby manages to be also the same kind of scripting language with like very little. I don't know, maybe it has a little more external work going on to make it faster, but still, it's even twice as fast as Python.

So, whatever! I'm sure a lot of you are going to yell at me in the comments for trashing Python, but really, like 400 times slower than optimized C++. Great!

But anyway, I was pretty impressed by V8 throughout this entire thing. JavaScript did a very impressive job for what it is. This was a video just comparing the speeds of languages. I think I did it pretty objectively.

If you want to see a language added, go ahead and leave a comment. If you want me to change a test, you can make a pull request or file an issue on this GitHub repository, if you use GitHub, or just leave a comment, whatever.

So, thanks for watching Maads 101. Subscribe, and goodbye!

More Articles

View All
Chasing Wolverines With Help From Ultra-Runners | National Geographic
[Music] This place is right on the fringe of so many important carnivore species’ habitat. In February of 2014, a camera trap here that the Department of Wildlife Resources had set up captured a wolverine on camera. That was the first time that had happen…
How To Get Out of Bed More Easily | Wake Up Early with ENERGY
Looks pretty good, what can I say? Bed. When you don’t feel like—do you have a hard time getting out of bed in the morning? Well, I don’t. So in this video, I’m gonna teach you how to be less like you and more like me. That’s not true; I still struggle o…
Relativity: Warping the Fabric of Spacetime
[Music] When someone is asked what they want to do with their life, we’re used to a familiar response: “I want to change the world. I want to make an impact.” While there are certainly many people who have made extraordinary contributions to society over …
A Warning For The 2023 Stock Market
What’s up, Graham? It’s guys here! So, 2023 is already off to an interesting start because, in just the last week alone, we’ve seen a woman go viral for buying a 1998 Ford Escort for 289 dollars a month for the next 84 months. A teacher was charged for ru…
The #1 cause of burnout is not what you think | Liz Wiseman for Big Think+
One of the things that comes up over and over in my research is the relationship between impact and burnout. In this moment in time that we find ourselves in, we’re dealing with epidemic levels of burnout in the workplace, burnout meaning you’re not just …
What's in Peanut Butter? | Ingredients With George Zaidan (Episode 7)
What’s in here? What does it do, and can I make it from scratch? Ingredients for the purposes of peanut butter: peanuts are just peanut oil and then all the stuff in here that is not peanut oil. So, things like sugars, starches, and proteins. When you bl…