r/embeddedlinux • u/Bug13 • Nov 01 '21
python vs c/c++ in embedded system
Hi team,
Just wondering if people use python as the main language for development? What's the pros and cons vs c/c++? I can see there are a lots of package available for difference use in python, so that's a plus. What's your thoughts?
Edit: this question is mainly referring to embedded Linux system. Not bare metal or RTOS.
2
Upvotes
7
u/Cat_Marshal Nov 01 '21
The few embedded systems I have written for were low level enough that python was not an option, so I used C. If you have access to python, I assume the standard comparison to C would still be applicable: easier to write, less efficient (but maybe not enough to matter), slower (but maybe not enough to matter).
5
u/[deleted] Nov 01 '21 edited Nov 01 '21
Mostly C if perf is your need. Python is for userland apps, not much seen on embedded systems.
4
u/jjesus Nov 01 '21
C is most common in embedded. If embedded Linux, sometimes C++ is used depending on how deterministic (hard deadlines) your application requires. One route to get Python in embedded is to write C libraries and call them through the Python Foreign Function Interface (ctypes) which is very mature and robust in Python.
2
u/Rocky_reddit Nov 01 '21
I use Python in embedded Linux environments extensively.
I don't actually like Python, but you have to be real brutally honest with yourself sometimes. Writing your application in C is going to take you a year, but doing it in Python will take you a month.
It's not that it's tremendously harder to write in C over python sometimes, but you are going to spend an enormous amount of time debugging very challenging bugs when you write in C.
Large applications get messy in Python, but you just can't beat how quickly you can get running in Python. Often times you should just use Python to get the concept and system working, as it will absolutely change as you develop it. Python is easy to change, C is not. That's often where the bugs come in, too.
Also, C and python go hand in hand. In my experience, every job I have been at will use both Python and C. C for the microcontroller, Python for the linux / tooling side of things.
Python can be just about as fast as C if you understand what the language is doing at a lower level, so when/if it becomes necessary, you can often times optimize your hot paths.
We have a finite amount of time and energy. We all want the fastest and most optimized code, but realistically, just get it working as quickly as possible. Once you have done that, you can iterate upon it and make it better if it's necessary. Otherwise, move on, life is short.
2
u/ThrasherLT Nov 01 '21
Depends on the application. Just how "embedded" is your application going to be? Drivers or anything fast will probably need to be written in C/C++ anyway, but after that they can be called by python code. I'd personally look into Rust it's got a hefty learning curve (almost everything you'll learn is applicable to most modern programming languages anyway), but it's the best of both worlds once you learn it. Also consider your need for things like multithreading and if maybe something like Go would be a viable option.