views:

2201

answers:

18

Hello StackOverflowers of the World,

In this age of many languages there seems to be a great language for just about every task and I find myself professionally struggling against a mantra of 'nothing but C is fast' where fast is really intended to mean 'fast enough'. I work with very rational open minded people who like comparing numbers and all I have is thoughts and opinion. Could you help me find my way past subjective opinions and into the "real world"?

Would you help me find research as to what if any other languages could be used for embedded and (Linux) systems programming? I very well could be pushing a false hypothesis and would greatly appreciate research to show me this. Could you please link or include good numbers so as to help keep the "that's just his/her opinion" comments to a minimum :)

EDIT:

  • memory is not a serious constraint
  • portability is not a serious concern
  • this is not a real time system
+1  A: 

Well, the truth is - not always. It seems .NET runtime (for instance. Any other runtime can be taken as an example) imposes several MBs of runtime overhead. (in RAM) If this is all you have then you are out of luck. JavaME seems to be more compact, but still it all depends on resources you have at your disposal.

EFraim
dotNet Compact framework is the mobile equivalent of JavaME.
Dykam
+2  A: 

Well, depending on the embedded platform, if memory constraints are an issue, you'll most likely need to use a non-garbage collected programming language.

C in this respect is likely the most well-known by the team and the most widely supported with available libraries and tools.

Sufian
+6  A: 

http://theunixgeek.blogspot.com/2008/09/c-vs-python-speed.html

http://wiki.python.org/moin/PythonSpeed/PerformanceTips (especially see Python is not C section)

http://scienceblogs.com/goodmath/2006/11/the_c_is_efficient_language_fa.php

I love Python. However, there are situations where you need real-time performance, especially in embedded systems. You also have severe memory constraints. A language like C gives you greater control over execution time and execution space.

So depending on what you are doing, C may very well be "better."

Christopher
thanks for the numbers, thats what this question is really looking for
Arthur Ulfeldt
www.pythononachip.org is a project that runs the PyMite VM on microcontrollers with no OS, no MMU and as little as 8 KB of RAM.
dwhall
+16  A: 

Using C for embedded systems has got some very good reasons, of which "performance" is only one of the minor. Embedded is very close to the hardware, you need manual memory adressing to communicate with hardware. All the APIs and SDKs are available for C mostly.

There are only a few platforms that can run a VM for Java or Mono which is partially due to the performance implications but also due to expensive implementation costs.

Johannes Rudolph
+37  A: 

In my experience, using C for embedded and systems programming isn't necessarily a performance issue - it's often a portability issue. C tends to be the most portable, well supported language on just about every platform, especially on embedded systems platforms.

If you wish to use something else in an embedded system, it's often a matter of figuring out what options are available, then determining whether the performance, memory consumption, library support, etc, are "good enough" for your situation.

Reed Copsey
Indeed. Embedded software tends to be written in C because that's how it's been done for years, and so embedded software engineers are more likely to be proficient in C than any other language. Accordingly, compiler producers are going to go for the language(s) which sell the best, namely C and possibly C++.
Steve Melnikoff
Of course, Python is written in C, as are many interpreted tools. With a little work you can use Python on many platforms.
Christopher
Yeah - Python can be. I've even used Lua (since it's pretty tiny in terms of overhead). It's the memory overhead and library support that can be an issue with things like pythong.
Reed Copsey
Yep - I agree with this entirely. At my former employer, there was of course a cultural ethos that was biased against anything other than C (and assembler), but the bottom line is that there are exceedingly few professional-grade toolsets for embedded systems that support anything but C. You *might* find toolset support for C++, and in the extremely rare case, Java. There are some vertically-stacked niche platforms for other languages (e.g., Erlang), but those are all but impossible to sell to management, particularly for existing systems.
Ben Collins
True dat -- if performance were your primary concern, then why stop at C? Write a first pass in C (compilers are pretty good, so that's a good starting point), and then go over the assembly by hand. Many systems have efficiency tricks you can do which aren't generally supported by the compiler.
Ken
@Ben Collins - GreenHills compiler has excellent support for C and C++, but it's expensive as rip. It is basically the Visual Studio of the embedded world in some aspects as far as powerful tools capability.
DoxaLogos
+3  A: 

I'm not really a systems/embedded programmer, but it seems to me that embedded programs generally need deterministic performance - that immediately rules out many garbage collected languages, because they are not deterministic in general. However, there has been work on deterministic garbage collection (for example, Metronome for Java: http://www.ibm.com/developerworks/java/library/j-rtj4/index.html)

The issue is one of constraints - do the languages/runtimes meet the deterministic, memory usage, etc requirements.

Nathan
Removing the GC doesn't fix the problem, malloc/free are non-deterministic. The D programming language allows you to disable the GC so it doesn't run during critical code.
he_the_great
Software for the lower end of embedded processors tends not to use malloc/free, but it is likely to use interrupts triggered by external events, which is unpredictable, and hence non-deterministic - regardless of the language used.
Steve Melnikoff
typically, malloc/free are disallowed in deterministic systems(or they are rewritten). @Steve: you can create deterministic interrupt handling, giving you bounds on time.
Paul Nathan
+12  A: 

Apart from performance, there is another consideration: you'll most likely be dealing with low level APIs that were designed to be used in C or C++. If you cannot use some SDK, you'll only get yourself in trouble instead of saving time with developing using a higher level language. At the very least, you'll end up redoing a bunch of function declarations and constant definitions :P

Thorarin
+2  A: 

Here are a couple articles that compare C# to C++ :

http://systematicgaming.wordpress.com/2009/01/03/performance-c-vs-c/

http://journal.stuffwithstuff.com/2009/01/03/debunking-c-vs-c-performance/

Not exactly what you asked for as it doesn't have a focus on embedded C programming. But it's interesting nonetheless. The first one demonstrates the performance of C++ and the benefits of using "unsafe" code for processor intensive tasks. The second one somewhat debunks the first one and shows that if you write the C# code a little differently then the performance is almost the same.

So I will say that C or C++ can be the clear winner in terms of performance in many cases. But often times the margin is slim. Whether to use C or not is another topic altogether. In my opinion it really should depend on the task at hand. But in embedded systems you often don't have much of a choice.

Steve Wortham
or you could say "shows that if you write the C# code a little differently then the performance is still slower". Not as slow as before, but nevertheless slower. Now, on my 3xcore, 4GB desktop PC that may not mean I'll ever notice a difference... but on a 10Mhz embedded CPU with 64Mb RAM the difference is likely to be tremendously significant.
gbjbaanb
Actually if you read that 2nd article he wasn't able to determine which was faster for this particular task (they were that close). But I agree -- C# has a lot more overhead and will not be nearly as impressive in low-powered embedded systems.
Steve Wortham
+7  A: 

There are several benchmarks on the web between different languages. Most of them you will find a C or C++ implementation at the top as they give you more control to really optimize things.

The Computer Language Benchmarks Game

Peter Olsson
+1 for the good link
Gabe
-1 for linking to measurements that haven't been updated for at least a year rather than a good link to the home page or the updated measurements.
igouy
Thanks, fixed the link.
Peter Olsson
+4  A: 

Embedded systems = ADA;

ADA is a Fast secure language that has data checking built in everywhere. It is what the auto pilots in airplanes are programmed in.

EDIT:
ADA vs. C

WolfmanDragon
I have used ADA in the past and would love to use it now, but I cannot find a version that will produce code to run in my 2K ROM/256 byte RAM environment.
Ian
Embedded systems is a broad range of applications. ADA is indeed a very strong and secure language which is used a lot in aviation absolutely has its strong points. But I haven't seen it in embedded systems consumer electronics, telecommunications and automotive infotainment yet. So I find your statement rather bold.
Adriaan
@Adriaan I have seen ADA on several consumer embedded linux systems, maybe just because of the distribution they chose?
Jiaaro
@Adriaan, you misunderstood what I was trying to say. ADA is the high level language that was designed for embedded systems and mission critical systems. It should be the high level language of choice on embedded systems. Yes, my statement was Bold and I stick by it.
WolfmanDragon
"ADA" isn't an acronym, but a name. Should be "Ada". (But I won't edit the answer, since this is a common mistake for which this comment might help.)
DarenW
+5  A: 

It's hard to argue against C (or other procedure languages like Pascal, Modula-2, Ada) and assembly for embedded. There is a large history of success with those languages. Generally, you want to remove the risk of the unknown. Trying to use anything other than C or assembly, in my opinion, is an unknown. Having said that, there's nothing wrong with a mixed model where you use one of the Schemes that go to C, or Python or Lua or JavaScript as a scripting language.

What you want is the ability to quickly and easily go to C when you have to.

If you convince the team to go with something that is unproven to them, the project is your cookie. If it crumbles, it'll likely be seen as your fault.

Nosredna
+1  A: 

A couple people have mentioned Lua. People I know who have worked with embedded systems have said Lua is useful, but it's not really its own language per se but more of a library that can be embedded in C. It is targetted towards use in embedded systems and generally you'll want to call Lua code from C. But pure C makes for simpler (though not necessarily easier) maintenance, since everyone knows it.

Brian
+1  A: 

You may want to look at The D Programming Language. It could use some performance tuning, as there are some areas python can outperform it. I can't really point you to benchmarking comparisons since haven't been keeping a list, but as pointed to by Peter Olsson, Benchmarks & Language Implementations has D Digital Mars.

You will probably also want to look at these lovely questions:

he_the_great
+16  A: 

"Nothing but C is fast [enough]" is an early optimisation and wrong for all the reasons that early optimisations are wrong. If your system has enough complexity that something other than C is desirable, then there will be parts of the system that must be "fast enough" and parts with lighter constraints. If writing your code in Python (for example) will get the project finished faster, with fewer bugs, then you can follow up with some C or assembly code to speed up the time-critical parts.

Even if it turns out that the entire code must be written in C or assembly to meet the performance requirements, prototyping in a language like Python can have real benefits. You can take your working Python prototype and gradually replace parts with C code until you reach the necessary performance.

So, use the tools that let you get the development work done most correctly and most quickly, then use real data to determine where you need to optimize. It could be that C is the most appropriate tool to start with sometimes, but certainly not always, even in embedded systems.

Neil
+1 for a good opening line!
Arthur Ulfeldt
If all the code were written in C, by great C programmers, the world would be a better place.
Andrei Ciobanu
+7  A: 

For C:

  • C is often the only language that is supported by compilers for a processors.
  • Most of the libraries and example code is probability also in C.
  • Most embedded developers have years of C experience but very little experience in anything else.
  • Allows direct hardware interfacing and manual memory management.
  • Easy integration with assembly language.

C is going to be around for many years to come. In embedded development its a monopoly that smothers any attempt at change. A language that need a VM like Java or Lua is never going to go mainstream in the embedded environment. A compiled language might stand a chance if it provide compelling new features over C.

Gerhard
+2  A: 

C really is your best choice. There is a difference for writing portable C code and getting too deep into the ghee whiz features of a specific compiler or corner cases of the language (all of which should be avoided). But portability across compilers and compiler versions. The number of employees that will be capable of developing for or maintaining the code. The compilers are going to have an easier time with it and produce better, cleaner, and more reliable code. C is not going anywhere, with all the new languages being designed to fix the flaws in all the prior languages, C with all the flaws these new languages are trying to fix still stands strong.

dwelch
+5  A: 

This recent, interesting article by Michael Barr talks about the use of C, C++, assembler and other languages in embedded systems, and includes a graph showing the relative usage of each.

EDIT: And here's another article, fittingly entitled, "Poor reasons for rejecting C++".

Steve Melnikoff
+1 Very nice article.
Andrei Ciobanu
+3  A: 

C is ubiquitous, available for almost any architecture, usually from day-one of a processors availability. C++ is a close second. If your system can support C++ and you have the necessary expertise, use it in preference to C - it is all that C is, and more, so there are few reasons for not using it.

C++ is a larger language, and there are constructs and techniques supported that may consume resources or behave in unacceptable ways in an embedded system, but that is not a reason not to use the language, but rather how to use it appropriately.

Java, and C# on Micro.Net or WinCE may be viable alternatives for non-real-time.

Clifford

Clifford