rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

C vs Python for GPIO

Sun Nov 11, 2012 12:50 pm

I recently decided that the speed difference I'd see using C would be worth switching over from Python, but now I'm not so sure. I've found two shortcomings of C that I'm not sure I can live with, mostly because I'm a whiny baby, and one that I think is a real show stopper.

First, is the lack of an easy way to find the length of an array. I know that you would use something like

Code: Select all

sizeof(someintarray)/sizeof(int)
but that seems clumsy to me. But I wouldn't even need that if--and this is my second point--C had a foreach loop. I often find myself looping over the elements in an array, and would rather do it using the equivalent of a foreach loop.

Lastly, and this is the show stopper, is the lack of any easy way to use dynamic arrays. From what I've seen online, this is a complicated process that uses pointers and structures to pull it off.

To be clear, I'm not bitching, moaning or whining here. I'm looking for advice. Please convince me that C is still a better alternative than Python, and help me see how each of these items doesn't really matter, or can be overcome. I still want to make the switch to C, but can't see how it's worth it for me. Show me the errors of my ways.

Thanks,
Rick
There are 10 types of people in this world. Those that understand binary, and those that don't.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: C vs Python for GPIO

Sun Nov 11, 2012 1:38 pm

rickseiden wrote:I recently decided that the speed difference I'd see using C would be worth switching over from Python, but now I'm not so sure. I've found two shortcomings of C that I'm not sure I can live with, mostly because I'm a whiny baby, and one that I think is a real show stopper.

First, is the lack of an easy way to find the length of an array. I know that you would use something like

Code: Select all

sizeof(someintarray)/sizeof(int)
Well this is ONLY calculated once at compile time assuming your array is of int's.

You could always try C++ for objects etc

Each language is different and has its quirks, limitations and better points. C is a lower level language so to get the speed you do some of the other work like equivalent of foreach only when you need to not all the time.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: C vs Python for GPIO

Sun Nov 11, 2012 6:34 pm

@rickseiden: If you don't want to deal with ``struct``\s and pointers then C is not for you. :-)

The C standard library is very limited compared to the amount of data structures and algorithms and building blocks you get with high level languages. But there are libraries that offer some of that stuff. For example the glib has dynamic arrays: http://developer.gnome.org/glib/stable/glib-Arrays.html

Code: Select all

while not self.asleep():
    sheep += 1

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: C vs Python for GPIO

Sun Nov 11, 2012 8:00 pm

Is it possible to use C++ instead of C? I'm using WIringPi, and I don't know if that's compatible with C++, but I do know that C++ is a little higher up on the language scale, making it a little easier, and therefore, a little better for whiners like me. :)
There are 10 types of people in this world. Those that understand binary, and those that don't.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20270
Joined: Sat Jul 30, 2011 7:41 pm

Re: C vs Python for GPIO

Sun Nov 11, 2012 8:17 pm

You can actually mix C and C++ code (not in the same file -- at link time). And C++ compilers are now good enough that there isn't much of a performance hit compared with C when using the more structured stuff (classes etc).

I use C all the time, and have used C++ extensively in the past - I'd go for C++ if you want a decent combination of speed and ease of use (others WILL disagree).
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: C vs Python for GPIO

Sun Nov 11, 2012 8:55 pm

Count me under others that will disagree. ;-)

I don't think C++ is easier than C. I find it *much* more complex and hard to get all the details right. Having C as ”platform independent macro assembler” on one side and high level languages like C#, Java, Python, Ruby on the other, C++ always seems to me the baroque beast in the middle which combines the worst of both sides of the spectrum.

Now I will look for my fire proof underwear and run for cover — surely I've stirred up some religious rage somewhere out there… :-D

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
poglad
Posts: 100
Joined: Tue Jul 31, 2012 8:47 am
Location: Aberdeen, Scotland
Contact: Website

Re: C vs Python for GPIO

Mon Nov 12, 2012 1:50 pm

I haven't used C++ since the 90s, and really got annoyed with header files etc.

Anyways - the point is that C is great for lower-level stuff like speedy access to GPIO. Doing a foreach, or treating an array as an object with properties like Length is really higher level than C is aimed at.

Have you considered moving your speed-critical code into C and keeping the rest in Python? I'm pretty certain it's straightforward to call out to a C library. You'd probably be able to call out to it from other languages too.

Emphursis
Posts: 8
Joined: Wed Sep 12, 2012 7:25 pm

Re: C vs Python for GPIO

Mon Nov 12, 2012 2:08 pm

rickseiden wrote:But I wouldn't even need that if--and this is my second point--C had a foreach loop. I often find myself looping over the elements in an array, and would rather do it using the equivalent of a foreach loop.
Unless I am missing something (possible!), could you not just use a for loop?

Code: Select all

for(i=0;i<=numofelements;i++)
{
    // Your code here
}

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: C vs Python for GPIO

Mon Nov 12, 2012 2:12 pm

That's why I called myself whiny. I want a foreach type structure, even though I don't HAVE to have one. :roll: at self
There are 10 types of people in this world. Those that understand binary, and those that don't.

kimwatson
Posts: 4
Joined: Fri Aug 17, 2012 3:27 pm
Location: Bordeaux, France

Re: C vs Python for GPIO

Mon Nov 12, 2012 2:30 pm

Just use C++ (if compatible with your aims), and the standard library (optionally). I also see boost is available for the RP. This brings a whole bunch of love.

If you want speed, you need to understand how the computer works.

Do you really need dynamic arrays? Would a fixed size array serve your purpose just as well? Why do you really need the foreach syntactic sugar. Just use the standard for loop.

There's an incredible amount of resources which you can draw upon out there for C/C++. And most problems such as the ones you mention, have answers, and well implemented solutions.

Eventually, it all comes back to C/C++. And if you know how it works, you'll be less likely to waste time working around having to learn it.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20270
Joined: Sat Jul 30, 2011 7:41 pm

Re: C vs Python for GPIO

Mon Nov 12, 2012 4:37 pm

BlackJack wrote:Count me under others that will disagree. ;-)

I don't think C++ is easier than C. I find it *much* more complex and hard to get all the details right. Having C as ”platform independent macro assembler” on one side and high level languages like C#, Java, Python, Ruby on the other, C++ always seems to me the baroque beast in the middle which combines the worst of both sides of the spectrum.

Now I will look for my fire proof underwear and run for cover — surely I've stirred up some religious rage somewhere out there… :-D
The languages you mention all suffer from being much slower that C/C++, meaning that C++ isn't really in the middle - it's much further to the C end, since it is really a superset of C, so you can write C code in C++ and you get C levels of speed, but you can still use 'higher' level constructs as classes etc. And as someone else wrote, you can always use Boost if you want Baroque!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

bbond007
Posts: 73
Joined: Sun Nov 04, 2012 6:10 pm

Re: C vs Python for GPIO

Mon Nov 12, 2012 5:42 pm

here is is an example of what you are asking...

I was able to come up with this in about 5 minutes and i'm not even close to being a good c programmer.

#include <stdio.h>
#include <stdlib.h>


typedef struct _tMyType
{
int uniqueID;
char data[100];
} tMyType, * pMyType;

int main()
{
int iRecTotal = 100;
pMyType myArray = NULL;
myArray = malloc(sizeof(tMyType) * iRecTotal);

int index;
for (index = 0; index < iRecTotal; index++)
{
myArray[index].uniqueID = index;
sprintf(myArray[index].data, "TEST:%d", index);
}

for (index = 0; index < iRecTotal; index++)
printf("%d -> %s\n", myArray[index].uniqueID, myArray[index].data);

free(myArray);
}

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: C vs Python for GPIO

Mon Nov 12, 2012 6:27 pm

@jamesh: I did not focus on execution speed when I put C++ in the middle but on language features like syntactic sugar, OOP, foreach loops, automatic memory management, contents of the standard library and so on.

I favor developement speed over execution speed as long as it executes fast enough. Which it is for me with the high level languages usually. If it is not I fall back to C. But just for the parts that are actually too slow.

@rickseiden: A nice language based on C syntax that compiles to native executables is the D programming language. Unfortunately Debian Wheezy still packages just the compiler for version 1 of D (package `gdc-v1`). It has static and dynamic arrays, a ``foreach`` keyword, classes, operator overloading, garbage collection *and* deterministic scope based memory management (a.k.a. RAII). It feels much more like Java or Python but can go down to the metal if needed.

Another option might be the Vala programming language, which is build around ``glib`` and removes the need of the boiler plate you have to use in C to use it.

Code: Select all

while not self.asleep():
    sheep += 1

kimwatson
Posts: 4
Joined: Fri Aug 17, 2012 3:27 pm
Location: Bordeaux, France

Re: C vs Python for GPIO

Mon Nov 12, 2012 6:40 pm

Yeah, I wouldn't touch C unless you really need to. It's got lots of old fashioned constraints which you'll not like (easy to see with a quick google) - it's a lightyear away from that nice little warm foreach setting you mention. :)

Just work with C++. It's cool, and modern. Nothing you can't do. And it'll perform as well as C.

The most recent C++ 11 standard (maybe even the previous 03 standard) has foreach type support like you mentioned.

Also, the previous example showcases a static array. It's not dynamic. Meaning you can't resize it at runtime without allocating a new array, copying the memory from old to new, and deallocating the old array.

kimwatson
Posts: 4
Joined: Fri Aug 17, 2012 3:27 pm
Location: Bordeaux, France

Re: C vs Python for GPIO

Mon Nov 12, 2012 6:49 pm

Regarding the other languages you mention. IMO if you are going to learn a new language, you might as well pick one that's used professionally across many industries. For example, we use C++ in all high production games (consoles, pc, etc). Never touch C. If performance required (in rare cases), it's assembly. I don't discount the other languages, but, perhaps prioritise learning/using those which benefit you not just in what you are doing now, but what you might wish to do professionally in the future.

dextrus
Posts: 119
Joined: Tue Jan 24, 2012 10:10 pm
Location: Eastleigh, Hampshire
Contact: Website

Re: C vs Python for GPIO

Mon Nov 12, 2012 7:58 pm

Python shouldn't be that slow, especially for serial comms. Are you trying to do something mental with it?

/Dextrus
Have more FUN with your Pi. Visit www.pi-fun.com

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: C vs Python for GPIO

Mon Nov 12, 2012 8:49 pm

Multiplexing and Charlieplexing are flashy, so I was hoping that C would be better. If I can use wiringPi with C++, I'm all for that.
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C vs Python for GPIO

Tue Nov 13, 2012 12:00 pm

rickseiden wrote:Multiplexing and Charlieplexing are flashy, so I was hoping that C would be better. If I can use wiringPi with C++, I'm all for that.
wiringPi is usable from c++

-Gordon
--
Gordons projects: https://projects.drogon.net/

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: C vs Python for GPIO

Tue Nov 13, 2012 12:07 pm

[email protected] wrote:
wiringPi is usable from c++

-Gordon
Thanks, Gordon. Coming from you, it's a sure thing! Now I just need to figure that part out :)
There are 10 types of people in this world. Those that understand binary, and those that don't.

Narishma
Posts: 151
Joined: Wed Nov 23, 2011 1:29 pm

Re: C vs Python for GPIO

Tue Nov 13, 2012 3:21 pm

Well, any C library is usable from C++. And you use them exactly the same way you would in C...

User avatar
paddyg
Posts: 2161
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: C vs Python for GPIO

Tue Nov 13, 2012 4:58 pm

or stick to python then use cython on the functions that would give you greatest gain. It seems to work fine on RPi
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
johnbeetem
Posts: 944
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Coast
Contact: Website

Re: C vs Python for GPIO

Tue Nov 13, 2012 7:38 pm

I think of C as portable assembly language. You are almost at the bare metal of the machine. The source-to-object-code mapping isn't one-to-one like assembly language, but the mapping is pretty straightforward. So if you like being close to the machine but want portability and the convenience of high-level notations, C is a good choice.

High-level dynamic programming languages that defer types to run-time are seductive, and some people just don't want to pick up after themselves and need automatic garbage collection. If that's how you want to interact with your computer and performance isn't critical, go for it.

It's mostly a matter of preference, and will vary from application to application.

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: C vs Python for GPIO

Tue Nov 13, 2012 9:03 pm

Narishma wrote:Well, any C library is usable from C++. And you use them exactly the same way you would in C...
Almost.
The calling sequence is different. One big reason is that C++ allows multiple functions by the same name to have different parameters.
To call a C function from C++ you must use
extern "C"

Many libraries look at the pre-processor flag to determine if the environment is C or C++ and adjust accordingly.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: C vs Python for GPIO

Wed Nov 14, 2012 12:23 pm

Yfory wrote:
rickseiden wrote:Please convince me that C is still a better alternative than Python, and help me see how each of these items doesn't really matter, or can be overcome.
Reading data from a light sensor circuit (as I did here), the load put on my Pi is significantly higher when running under Python compared to C. I am not a Python expert at this point, so there may be a way to reduce that load issue. In general I have noticed this is a big difference with Python.
There's a good reason. I have a VoiceberryPi project I did that had a high cpu utilization under Python. Maybe redoing it under C/C++ would reduce that.
There are 10 types of people in this world. Those that understand binary, and those that don't.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 5 guests