C, C++, Java and Python speed comparison on a Raspberry Pi 3

preview_player
Показать описание
Each program counts from 0 to 1000.000.000 and displays the elapsed time after completion.

Results on my Raspberry Pi 3:
C: 6,722 sec. (first run), 6,719 sec. (second run)
C++: 6,749 sec. (first run), 6,717 sec. (second run)
Java: 9,234 sec. (first run), 9,221 sec. (second run)
Python: 1102,637 sec.
Рекомендации по теме
Комментарии
Автор

yeaaa, C/C++ is winner, agree, C builder is the most powerful language

WahyuExano
Автор

I think its better to test by trying some complex operations such as Finding prime numbers within 2000 instead of blank loop.

DimensionalShiv
Автор

I wonder what would JavaScript/nodeJS do?! 😂

abdallah-abdelazim
Автор

And that's where optimisation for each language would come in play. That is why you need to know how to write code in each language, not only "script" it using some generic way. While C or C++ complied first, then run as native code, Java or python does not need to be complied at all, but will be interpreted during the execution, it is obviously slower.
However, if you know how to write code in Java or Phyton properly, this would significantly reduce the execution time.
I.e. In C there is no string defined, only "characters as an array". This means if you want to use String a = "aaa" + "bbb" it wont work until you use some library for it (well in C there is no class, so you will end up using C++ anyway, again with some library). The speed and memory footprint will depends on the library you are using. While, if you are using Java or Pyhton you do have "built-in" string, however it is immutable. This means if you do string a = "1" + "2" it will create and drop several strings before you will get the final one. If you are using stringbuilder, then it will be only 1, much faster with less memory footprint.
When you are using java, or phytoh, c#, etc... you should use while x != 0 because it has a separate instruction to compare a value to zero, the result can be over 10x faster. Also, when you are using var (any type) instead of unsigned int (proper type), the difference are again significant. You cannot do this in C, so you cannot make it worst, but everything you get to make your code easier to write will make either the building time or the execution time slower. 

Think this way:  
- if you missed to tell to the computer that a variable is an integer, then your computer needs to find it out, which will be slower. 
- if you reuse the same class or it's variable multiple times, think about to first assign it to a local temp value (which will be on your stack, very quick to access) while if you always use it via the "class" it will every time "look it up", depending on your languages it might copy it every time, so you will end up a lot slower execution
- some languages passing values by value (which means it will duplicate it every time you pass it to a function) while others passing by reference (so it will use the very same variable, no copy required, much faster and no extra memory used), while some allows you to mix these.

Anything that you don't do in a language but it is important for the machine code will make your code either build or execute slower.
This is why it is good to know ASM, machine codes, C or any other low level coding, because it will help you to understand what and when is important to strict coding or lazy one. (lazy does not means it is bad, if you know it wont be a problem on that part of the code).

Anyway, your test shows only that if you follow 1 very "generic" coding style on multiple languages, c will win - simply because it does not allow you to make that much lazy coding.
And you also missed one: how about ASM or simply write it in machine code, those would kill C as well.

Don't forget also, that most of the people who writes code in python or in java would give up their project after 1 months if they would do it in C or C++ due to the complexity of building, linking, makefiles and error handling. There are many trade off you need to consider when you are picking a languages. If you write a code for a simple problem or you dont have time to setup a build system, install the right gcc, debugger, IDE, (= development environment), or you dont want to deal with linking and project setup, it is a lot quicker to use python, java or C#, vb... (quite long list here)
However if your goal is to make a code very fast or you know you need to have long time project management anyway (ie. image processing, games, etc) then you should consider C++. If you want to make code very small in memory (typically HW development, firmwares, drivers) then use C instead of C++. Use can also use ASM or a mix. (C allows you to mix code very easily with ASM as well for better optimisation)

Don't forget, there are also many tricks and tools to make your code faster or use less memory. There are many Python libraries written in C++, which means the time will be different from C++ code only for interfacing with the library code, but you dont need to deal with those generic issues as with C++.
You can also compile any java, python, c# etc code into native, which then again a huge difference, but you need to deal with similar issues as it would be a C++ code (building).

So pick the language based on your goal and not what you think it's faster. Faster is always relative!

Creativepudding
Автор

If you use list comprehension in python it runs optimized C loop. Also you need to ensure that you are running CPython and not other interpreters.
If you do Abstract Syntax Tree mapping you can see that Python runs fairly close to C minus the top level interpreter functions. There is also a way to run native C inside python with exec() function.
However, it is true that for most people that don't delve deep into python and algo optimization, it will likely run much slower than other languages. "Easy to learn, difficult to master".
Beauty of python is that write time is great and if you couple it with C/C++ internals (some boilerplate code there), you can write grab the best of two worlds.

viktormikhaltsevich
Автор

Yeah I don't regret a bit that I left python in the past after learning the basics of programming...
And I learnt the fundamentals of programming even better when I moved away from python

MarSem
Автор

What flags were used to build the C++? As the variable "i" is not declared as volatile, the compiler optimized should completely remove that unused loop for release build with -O3 as optimization flag.

rafaelfassi
Автор

Holy crap that is shocking...I love python...that is crazy it is so slow...

kriskizlyk
Автор

1100 sec for python? did I see that right?? try again..

dkenny
Автор

Bet no one was expecting Java to only be a few seconds slower than the 2 infamous languages, heh

theshermantanker
Автор

You used for loop in every other languages, but for Python you used while loop. That's unfair totally.

souvikghosh
Автор

Do everyone a favor and type CTRL-R in that command line

pisoiorfan
Автор

what i can do in machine learning using python, you'll spend 1 week to do it with c or c++

Tijan
Автор

I love python but God it's so slow xD

Maniclout
Автор

Compile the Java to a GraalVM native image, you will find that it's much faster now.

slr
Автор

The C code isn't optimal. I can be sped up.

RockTo
Автор

What kind of jave version you use and what kind of jvm

orlovskyconsulting
Автор

Well, pretty obvious that Java was going to be slower than C/C++ since it's an interpreted language. Instead, it's strange that python was that slow.

losav
Автор

python compile at runtime and that's a raspberry

aasaaaaabsa
Автор

You should compile it for python or run with pypy... will be much faster

syzer