views:

139

answers:

8

Maybe this has been asked before, but I couldn't find it. My question is simple: Does it make sense to write an application in higher level languages (Java, C#, Python) and time/performance-critical functions in C? Or at this point unless you do very low level OS/game/sensor programming it is all the same to have a full, say, Java application?

+7  A: 

It makes sense if you a) notice a performance issue, AND b) use performance measurements to locate where the problem occurs, AND c) can't achieve the desired performance by modifying the existing code.

If any of these items don't apply, then it's probably premature optimization.

Cogwheel - Matthew Orlando
They should also consider how often the native code will be invoked as you take a performance hit marshaling calls between managed and native code.
ChaosPandion
Indeed. Once you DO make the decision to try out C, you need to do more measurements to make sure it was the right decision ;)
Cogwheel - Matthew Orlando
@Cogwheel - Seriously, if there is any universal mantra for a programmer it is *"Measure first then decide."*.
ChaosPandion
... if performance is an issue for the project ... otherwise the mantra could be different, to achieve great maintainability and fast development, e.g.
ShinTakezou
A: 

I suggest you read Cliff Click's excellent Java vs. C Performance....Again.. It outlines many points of comparison between Java and C++.

Predictably, the conclusion is that it depends, but it's a worthwhile read.

Robert Munteanu
rlb.usa
@rlb.usa: I believe that Java/C is a good start to see that the border between them is quite blury.
Robert Munteanu
Read it, but take it with a grain of salt. If you read carefully, it's clear that this is by a guy who started out trying to claim that Java was universally superior, and got called on a number of points where he was clearly wrong. A lot of the "it depends" points are places he was also *probably* wrong, but solid proof is hard to find.
Jerry Coffin
@Jerry - I always read blog posts with a bit of skepticism until they prove where their "facts" came from.
ChaosPandion
@ChaosPandion: Some skepticism is always reasonable, of course -- but this one appears to merit a bit more than usual...
Jerry Coffin
@ChaosPandion: I suggest you take a look into who Cliff Click is. He does know _a lot_ about Java and machine-level execution. He's not a run-of-the-mill blogger.
Robert Munteanu
+1  A: 

Usually your preferred language will do whatever you need it to in acceptable time (er, blazing fast).

Sure, critical time/performance functions can be written in a "more optimal/suitable" language like C or assembly - but whether it will actually make things faster is another story. There are laws that govern how much actual/overall speed-up that you'll get, specifically Amdahs Law and (diminishing returns) .

To answer your question, it only makes sense to rewrite these critical functions in lower languages if there is good enough speed-up to warrant the extra work.

rlb.usa
A: 

You can only really answer this on a case by case basis and without reference to what you are doing it's impossible to answer.

But maybe what you actually want here is some kind of sanity check to ensure that this approach isn't crazy to consider. I have worked on tools ranging from a very large graphics application (~ million lines) to relatively small physical simulation engines (~10000 lines) there were written just as you describe: Python on the outside for interface (both for API and for GUI), C/C++ on the inside for the heavy lifting. They all benefited from this division of responsibility.

A: 

This is done especially with scripting languages. Things that come to mind are games made in Python. Most of the time Python is too slow for some of the more number crunching aspects of games and they make this a C module for speed. Be sure that you actually need the speed though and that number crunching is your performance bottleneck and not a general algorithm issue. Doing a brute-force search over a list is going to be slow in both C and Python.

Earlz
+3  A: 

If you are fluent and productive in a higher level language such a Python and Lua, then by all means start writing in that language. Look for bottlenecks if and when they exist.

lhf
Use [language X Y] instead! Isn't helpful, and doesn't answer his question.
rlb.usa
I did not say instead. I don't understand the down vote. I mean to be helpful.
lhf
I think rlb.usa completely misunderstood your answer.
ShinTakezou
A: 

I'd say that it depends a lot on your application. What sort of performance is important? short startup-time? high throughput? low latency? Is it important that response time is always predictable? Is the application short lived or does it run for long periods of time?

Java can give you high throughput, but occasional short freezes while doing garbage collection. C# is probably similar. Python, well the performance there will often lag behind the others for anything not written in C (some things ARE written in C, even if you didn't do it yourself).

So as others said. It depends.

But as always with performance: Measure first, optimize when you know you need to.

Mattias Nilsson
+1  A: 

speed can be quite similar with things like C#.

What is tricky is latency. So if you want to write something which you know takes < 10ms then C is reasonably predictable (ignoring whatever variability your operating system might introduce).

Having said that for very tight long loops (image processing for example), things like C/C++ can offer some speed up. You can get quite reasonable performance out of C#, you do have to be careful how you program it though, but I have found in general, you can still squeeze more out of C/C++

Keith Nicholas