The main reason to use C++ over python for your Embedded Systems projects
I had the opportunity to develop a product using raspberry pi and in turn saw the performance difference between a c++ project and a python project which essentially do the same thing.
I tried out something and I was shocked by the results. I wrote two programs — one for c++ and one for python to do essentially the same thing and this is how it went.
#include <iostream>int main() {
long a=0;
for(int i=0; i<1000000000; i++) {
a++;
}
}
In this cpp file, I incremented a value 1,000,000,000 times.
a = 0
for i in range(0, 1000000000):
a += 1
In this python file, I incremented a value 1,000,000,000 times.
I compiled the cpp file using g++ hello.cpp
and ran time ./a.out
. This was the result:
./a.out 1.70s user 0.00s system 99% cpu 1.704 total
I used the python interpreter to time the same using time python3 hello.py
. This was the result:
python3 hello.py 82.13s user 0.12s system 99% cpu 1:22.39 total
While I always knew that C++ has better performance than python, I did not expect this much difference. The total time for the process for C++ was 1.704 seconds and for Python, it was 1 minute 22.39 seconds. This is on my laptop which has way better CPU than a raspberry pi. C++ is almost 48 times more faster than Python in this case.
Therefore, whenever going for your Embedded Systems Projects, try C++, it might break a lot of bottlenecks you might be facing.