4

Let's say you have to implement a tool to efficiently solve an NP-hard problem, with unavoidable possible explosion of memory usage (the output size in some cases exponential to the input size) and you are particularly concerned about the performances of this tool at running time. The source code has also to be readable and understandable once the underlying theory is known, and this requirement is as important as the efficiency of the tool itself.

I personally think that 3 languages could be suitable for these three requirements: c++, scala, java. They all provide the right abstraction on data types that makes it possible to compare different structures or apply the same algorithms (which is also important) to different data types.

C++ has the advantage of being statically compiled and optimized, and with function inlining (if the data structures and algorithms are designed carefully) and other optimisation techniques it's possible to achieve a performance close to that of pure C while maintaining a fairly good readability. If you also put a lot of care in data representation you can optimise the cache performance, which can gain orders of magnitude in speed when the cache miss rate is low.

Java is instead JIT compiled, which allows to apply optimisations during runtime, and in this category of algorithms that could have different behaviours between different runs, that may be a plus. I fear instead that such an approach could suffer from garbage collector, however in the case of this algorithm it's common to continuously allocate memory and java heap performance is notoriously better than C/C++ and if you implement your own memory manager inside the language you could even achieve good efficiency. This approach instead is not able to inline method invocation (which induces a huge performance penalty) and doesn't give you control over the cache performance. Among the pros there's a better and cleaner syntax than C++.

My concerns about scala are more or less the same as Java, plus the fact that I can't control how the language is optimised unless I have a deep knowledge on the compiler and the standard library. But well: I get a very clean syntax :)

What's your take on the subject? Have you had to deal with this already? Would you implement an algorithm with such properties and requirements in any of these languages or would you suggest something else? How would you compare them?

hoheinzollern
  • 651
  • 6
  • 16
  • 1
    Probably better asked on http://programmers.stackexchange.com ? – Paul R Apr 11 '11 at 15:18
  • 1
    @PaulR No, why? This is a completely technical programming question and is absolutely on topic. Whoever voted “not a real question” also needs to re-read the question: this *is* a real question. It’s asking about advice in a fairly specific situation. – Konrad Rudolph Apr 11 '11 at 15:21
  • @Konrad: my understanding was that SO questions should be answerable with programming code, whereas this seems more like a vague subjective questions about programming methodology which can't be given any kind of definitive answer. – Paul R Apr 11 '11 at 15:23
  • 1
    Good question. I wonder if it should be migrated to CS Theory (http://cstheory.stackexchange.com/) or Programmers (http://programmers.stackexchange.com/) ? – rajah9 Apr 11 '11 at 15:26
  • 1
    @rajah Definitively not to CS Theory, as it is not research level theoretical CS. – abeln Apr 11 '11 at 15:28
  • 3
    I do believe this question belongs here. It's specific enough. CST is theory instead, and Programmers is about non-programming. – mafu Apr 11 '11 at 15:28
  • @Paul Lots of answers don’t require code. And this question isn’t really any more subjective than most questions, just as most questions don’t have one single “definitive” answer. The OP asks for specific technical advice. – Konrad Rudolph Apr 11 '11 at 15:33
  • @Konrad: OK, well it seems to me more like a general discussion topic than a specific programming question, but I'll bow to the majority verdict. – Paul R Apr 11 '11 at 15:36
  • 1
    @mafutrct Programmers is about "subjective questions on software development". IMHO this would belong there better . – abeln Apr 11 '11 at 15:37
  • 1
    I think this should go to programmers.SE. I agree not all questions have to be answerable with code, but they should at least have a single objective answer. Questions asking for broad advice and/or opinions (which may also turn out to be "subjective and argumentative") belong to programmers.SE. This question essentially falls into the class of "language X vs, language Y" questions - most of which are simply closed, let alone migrated to porgrammers.SE. – MAK Apr 11 '11 at 15:38

4 Answers4

5

Usually I’d say “C++” in a heartbeat. The secret being that C++ simply produces less (memory) garbage that needs managing.

On the other hand, your observation that

however in the case of this algorithm it's common to continuously allocate memory

is a hint that Java / Scala may actually be more suited. But then you could use a small object heap in C++ as well. Boost has one that uses the standard allocator interface, if memory serves.

Another advantage of C++ is obviously the use of abstraction without penalty through templates – i.e. that you can easily create generic algorithmic components that can interact without incurring a runtime overhead due to abstraction. In fact, you noted that

it's possible to achieve a performance close to that of pure C while maintaining a fairly good readability

– this is looking at things the wrong way: Templates allow C++ to achieve performance superior to that of C while still maintaining high abstraction.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Perhaps use one of the garbage collection libraries with C++? I believe that the performance of Java won't be enough, and Scala is too new for my tastes. – vonbrand Mar 08 '14 at 02:03
  • @vonbrand I can’t really judge on the respective performance in a real situation (it depends too much), but Java’s garbage collector is highly sophisticated and I’m not aware of anything comparable for C++. The commonly used Boehm GC isn’t bad but it’s not state of the art either. – Konrad Rudolph Mar 08 '14 at 10:24
2

D might be worth a look, seeing as how it tries to be a better C++.

Community
  • 1
  • 1
Martin DeMello
  • 11,876
  • 7
  • 49
  • 64
1

The languages you noticed were my first guesses as well.

Each language has a different take on how to handle specific issues like compilation, memory management and source code, but in theory, any of them should be fitting to your problem.

It is impossible to tell which is best, and there is likely no major difference if you are familiar enough with all of them to work around their respective quirks.

And obviously, if you actually find the need to optimize (I'm not sure if that's a given), that's possible in each language. Lower level languages obviously offer more options, but are also (far) more complex to actually improve.

A single note about C++ vs Java: This is really a holy war, and if you've followed the recent development you'll probably have your own opinion. I, for one, think Java offers enough good aspects to make up for its flaws, usually.

And a final note on C++ vs C: According to my knowledge, the difference usually amounts to a sufficiently low percentage to ignore this. It it doesn't make a difference for the source code, it's fine to go with C, if C++ could make for easier-to-read source code, go with C++. In any case, the choice is kind of negligible.

In the end, remember that money spent on a few hours of programming/optimizing this could as well go into slightly superior hardware to make up for missed tiny details.

It all boils down to: Any of your options is fine as long as you do it right (domain knowledge).

mafu
  • 31,798
  • 42
  • 154
  • 247
1

I would use a language which makes it very easy to work on the algorithm. Get the algorithm right and it could very easily outweigh any advantage from fine-tuning the wrong algorithm. Don't be scared to play around in a language normally thought of as slow in execution speed if that language makes it easier to express algorithmic ideas. It is usually much easier to transcribe the right algorithm into another language than it is to eek-out the last dregs of speed from the wrong algorithm in the fastest executing language.

So do it in a language you are comfortable with and which is expressive. You might surprise yourself and find that what is produced is fast enough!

Paddy3118
  • 4,704
  • 27
  • 38
  • What you say is certainly true, and a priority of course :) Indeed I didn't mention pure C at all, which is normally used for this kind of works. But good memory management and efficient data structures are also important, so I wont use interpreted languages for instance. – hoheinzollern Apr 29 '11 at 05:46
  • Some algorithms require good sorts or good associated arrays. These are areas where their implementation in certain interpreted languages is top-notch. – Paddy3118 May 17 '11 at 22:41
  • Don't knock the interpreted languages too quickly. Some very fast libraries can be embedded in, and used from scripting languages. – Paddy3118 May 17 '11 at 22:43