Python vs. C (C++)

Python vs. C/C++

When It Comes to Performance

Python vs. C/C++. There are many reasons for a programmer to use Python as a programming language. Python is easy to learn compared to many other programming languages like Java, C or C++, e.g. you don’t need to care much about memory management or data types.

But when it comes to performance then you might want to use C or C++ as your choice of programming language. Usually a C program is 10 to 100 times faster than the same program written in Python! This also means less power consumption on the CPU and other components and this is a very important argument when it comes to make Internet of Things!

Differences Between Python and C

Python is basically an interpreted language. That means that a program called an interpreter must run first, then reads your Python program and executes the commands there usually one by one.

A program written in C must be compiled in order to be able to run on a machine. Compiling means to translate the C program into the computer language. Once the program is compiled you have an executable file that can be executed directly by the physical hardware (CPU or processor). No intermediate program like an interpreter is needed. Therefore the overhead is less.

Less overhead means faster programs. If your program would need 60 seconds for something specific to do in Python it would need like 1 second in C and this means the hardware has less to do if you code in C. If the hardware has less to do, the overall system power consumption is less and this is important when you are going to make a smart thing that runs on battery.

I don’t want to say that you should never use Python. Python is good for many reasons. Program development in Python is much faster, easier and you usually don’t care about things like data types or how to deal with objects. It is usually more cost-effective. Python takes care about many things that you should take into consideration when programming in C. And in many cases the programs written in Python don’t run that long. And in the meanwhile there are libraries for Python written in C, compiled and ready to use.

Programming in C on Raspberry Pi

Basically when you are programming on a Raspberry Pi (you can buy one here: English, German) you have a native C/C++ compiler that comes with the Linux operating system. Linux is a great Open Source operating system that allows developers to use the machine to create make amazing projects using Open Source.

Accessing the GPIO pins in a C program is a bit tricky. I use a library called “libcurl4-openssl-dev” and I would recommend you to use it too. It makes it very easy to manipulate the GPIO pins.

So, have fun and let me know if you like to read more about C programming on Raspberry Pi. Feel free to tell me your opinion.

You may like to read about the man who invented the C programming language as well, Dennis Ritchie. Today his codes are running pretty much everywhere!