I'm embedded systems engineer, Mozilla rep, blogger and writer, Violinist. Founder of mookhtabar. In my area, the people call me a scientist but I call my self a thinker.

Are C/C++ programs faster than python programs? and why? [ Experiment with Raspberry pi board ]


Why do we need to use C/C++ programming language even we have Python programming language? even Python is was a big support language in Raspberry pi boards or in other boards?

To I can give you an answer about that, you need to look in the levels of the programming languages (Picture 1), and you can see that  C/C++ is High/Middle level languages and it directly after the Assembly language, so this means that  C/C++ is very fast when we compared with python or other languages from the Scripting/Interpreted languages.

I know this is a theoretical answer, and we need to prove this answer by experiment, I've written this article for that, I've designed a small experiment that helps to see the difference when we use C/C++ or Python.    

Picture 1 (source: link)
The experiment is about generating a signal, first time by C/C++ and the second time by Python, and We have to use the Raspberry pi board because it has pins we call it GPIO, by this pins we can see the signal when we connect an oscilloscope with it. This experiment we call it in general PWM (Pulse width modulation) generate, but in this experiment, we don't use the methods of PWM, we just send ON ofter that OFF and other that ON to the pin in loop or we change the status of the GPIO from high to low and from low to high without delay in loop, this experiment gives us the time to Raspberry pi board can change the status of the pin (GPIO) from hight to Low and vice versa by python and C/C++.  

I've been used Raspberry pi 3 model B and these two scripts: 

1- The script of python: I add it in the .py file.



import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)

while True:
    GPIO.output(3, 0)
    GPIO.output(3, 1)
 
 
2- The script of C: I add it in the .c file


#include 
#define PIN RPI_GPIO_P1_03

int main(int argc, char **argv){
 if(!bcm2835_init())return 1;
 bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP); 
 while(1){      
  bcm2835_gpio_set(PIN); 
  bcm2835_gpio_clr(PIN18);
 }
 return 0;
 }


I've connected the pin 3 and GND of Raspberry pi board with the chanal 1 of the oscilloscope and I click "Build the current file" after that click to "Run". I have run this scripts in Raspberry pi with raspbian OS. 

the results:

For python: 


For C/C++:


You can see that the frequency of the signal with python is 152.5 kHz (mean period = 6.556 us), and for C/C++ the frequency is 5.815 MHz (mean period = 171.9 ns). these results mean for maximum, Raspberry pi board (with the conditions of this experiment) can change the status of the pin from High to low and vice versa without delay and in the loop in 171.9 ns with C/C++ and in 6556 ns with Python. 

To calculate how C/C++ speed for this experiment we just calculate this operation:  6556 / 171.9 = 38.1384525887 
and we can just say 6556/171.9 = 39 
This means the signal output by C/C++ is 39 times faster than the signal output by python. 

I re-run this experiment much time but give me the same results, I found an article like this experiment get C/C++ is faster 110 times faster than python (see here: link), but it is used the pin (GPIO) 18 or PCM_CLK not pin 3. But in general, we found that the C/C++ is not just faster but it is so faster than python!!

But Why ??

In the past, I've read a book about Assembly with ARM processors (picture 2). Now, I don't know where is the part that I have read in it about when we write code with Hight level language it converts to assembly, and because most Scripting/Interpreted languages are written by C, these codes convert to C (like python) and then the C codes convert to assembly because the machine (Rasberry pi board or processor) understand assembly language only.  

In this operations of converted from language to other, the compiler creates lines of code more than the normal, like when you say "tell Hello to steve" to a person and that person say to steve "Michal said: Hello to you", so if you say directly to steve "hello" you just use 1 word but when you use an intermediary you will use 4 words to just say "Hello" to steve. 

picture 2 
I search in Google to see more answers about "Why C/C++ is faster?"  and I liked these two answers: 




Now, many people use python to make their projects but some times you need to use C/C++ to write the codes of your project because you need the faster language or you need to be near more to the machine language. 

From the Lab


No comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel