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.

Programming Raspberry pi with C/C++ [ the lesson 0 introduction and why C/C++ speed than python ]

For this tutorial, we learn how to use C/C++ programming language to programming or control Raspberry Pi, control the GPIO and others things. 

We start with a big question, Why we need use C/C++ if we have a python and it is big support language in it?
forgive you an answer about that, you need to look in this picture (the levels of the programming languages ), and so you look the C/C++ is the language directly after the assembly language, so this means the C/C++ is very fast when we compared with python or other languages.

 For test what the different between when programming with Python and programming with C or C++, we change the status of the GPIO from high to low and from high to low without delay, so that like PWM and can give us what the time of  raspberry pi change this status in python and C/C++, for general idea I create the same job in the two languages and I observe how Raspberry pi response.

For doing that, I write the two script below, I use Geany 1.24.1. 

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

import RPi.GPIO as GPIO

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

#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); 
 return 0;

the results:
Forget results, I start with python script. First add the pin 3 and GND in the chanal 1 of osilloscope. Second, I click "Build the current file" after that click to " Run ", and now I look the screen of osilloscope, I had a problems with osilloscope because the first time I use it, but I fix it :D I now we look to the pictures below.

The result for python:

 The result for C:

So for Python the frequance of the signal we get is 152.5 KHz with a period of 6.556 us. for C the frequancy is 5.815Mhz with 171.9 ns (the signal is not get but we look just the frequancy). So for this valures we see when 5815/152.5= 39, so for my experement the signal outuput from C is faster 39 times than the signal output from Python. for other experement, you can see in the link below, the C faster 110 times than python.

picture from the LAB

source of pictures:
picture1: link
source of information about the other test of speed:
source1: link

No comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel