Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: How much useful is C++ in algorithm research? (Read 12871 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How much useful is C++ in algorithm research?

Hi folks,


I was wondering... How come standard C programming language is used in algorithm research instead of C++ with all its fanciful classes and encapsulations?
How come the MPEG standardization committee release a source code based on standard C instead of C++ ? Of course, I also noticed that  most algorithm researchers actually prefer standard C instead of C++?


wkwai

How much useful is C++ in algorithm research?

Reply #1
Possibly because C has been around much longer (60's vs. 90's of C++), is much easier to implement compiler-wise and the correlation between the C code that you see / generated machine instructions is very high. And the C language as a simple (ok, in theory) procedural language has a very large "industry standard" label on it's forehead.

I understand that short functions (FFT and such) can be presented with C language just fine but on a large scale, I would prefer an object-oriented C++ approach.

However, if a presentation of an algorithm in C requires arrays and intense pointer arithmetics or resorts to some other trickery, I would prefer to see a container class version of it.

How much useful is C++ in algorithm research?

Reply #2
Ian Joyner's famous "C++?? : A Critique of C++" might explain some of the reasons why very many people prefer to avoid the C++ language in all cases if possible.
You can find the HTML version here:
http://burks.brighton.ac.uk/burks/pcinfo/p...pcrit/index.htm
IMHO it's a recommended reading for anyone interested in OO programming.

How much useful is C++ in algorithm research?

Reply #3
Wouldn't there be computational overheads in using C++ in computationally intensive routines?

wkwai

How much useful is C++ in algorithm research?

Reply #4
Quote
Wouldn't there be computational overheads in using C++ in computationally intensive routines?

wkwai

Nope. No one forces you to use the "slow constructs" in C++. Just code in a C-style function without objects and virtual functions, apply your own über-SSE2-inline assembler optimizations, and it will be just fast if not faster than it's C equivalent.

I don't understand why everyone hates C++ so deeply. I've read the critique about 100 times, I agree with some points but I still love C++. I must be nuts

How much useful is C++ in algorithm research?

Reply #5
Quote
Wouldn't there be computational overheads in using C++ in computationally intensive routines?


Probably not. A C-only compiler might be able to optimize more easily than a C++ one because it could make assumptions the latter cannot. But it probably highly depends on quality of implementation and will not make a real difference. Basic OO design is not that interesting for numeric computations since it must carry the usual C++ burden: temporaries. However you could take a look at modern template-oriented implementations like Blitz.


Off-topic here but i am interested:
Quote
I don't understand why everyone hates C++ so deeply. I've read the critique about 100 times, I agree with some points but I still love C++. I must be nuts 


Because C++ is a complex language, and the complexity of its features stems from trade-off that you must know/understand before using them. And most of people just don't care about that, code in C++ like in C with Classes and mess with it, before comparing the whole thing with another language with less apparent complexity they know better.

And I can agree with them depending on their positions. Should I have a bunch of not-so-skilled-neither-very-motivated programmers, I would fear any C++ code they manage to write.

Moreover, people do not know the same C++ language: most of them only know the Java like version, with classes, polymorphism & Co, from before 1995. Few know the one with early template support and STL normalized in 1998. And fewer know the recent template developments implemented in libraries like Boost or Loki and supported by a few compilers. I think the latest C++ specification was made in 1998, and we currently have only one compiler supporting it completely (Comeau). The fact that people use pre-98/broken compilers (like MSVC 6.0 for instance) is likely to have played a great role in this hatred. But the situation is going to change apart for the (debatable) export thing (MSVC 7.1 is really close to compliance, and GCC 3.2 has made great steps too).

However, I like C++ because of its complexity. Because to learn it you must understand things beyond mere syntactic details or basic OO design. In the end, learning C++ makes you learn things about compiler design and implementation.

How much useful is C++ in algorithm research?

Reply #6
Quote
Nope. No one forces you to use the "slow constructs" in C++. Just code in a C-style function without objects and virtual functions, apply your own über-SSE2-inline assembler optimizations, and it will be just fast if not faster than it's C equivalent.

I see.. without objects right? Does that mean that these routines should be place within the same class? I did some algorithm programming in OOP sometime ago, I was told that it will be alright as long as "the routines are kept within the same class".. Now this is a problem! For most algorithm, almost all routines are computationally intensive... and I ended up with only one class for the entire program.. This sort of defeat the purpose of encapsulation!


wkwai

How much useful is C++ in algorithm research?

Reply #7
Quote
The fact that people use pre-98/broken compilers (like MSVC 6.0 for instance) is likely to have played a great role in this hatred.

I have been using MSVC 6.0 for many years.. I do noticed that sometimes, there is some problem compiling codes with it.. I was told that it was most probably caused by me mixing C codes with C++ codes. I was advised to stick to standard C to avoid a lot of compilation problems.

It is great that you have pointed out the problem of pre-98/broken compilers issue.



wkwai

How much useful is C++ in algorithm research?

Reply #8
Quote
I see.. without objects right? Does that mean that these routines should be place within the same class? I did some algorithm programming in OOP sometime ago, I was told that it will be alright as long as "the routines are kept within the same class".. Now this is a problem! For most algorithm, almost all routines are computationally intensive... and I ended up with only one class for the entire program.. This sort of defeat the purpose of encapsulation!


wkwai

Important Fact: For most computationally intensive applications which are cpu bound (this excludes those which use e.g 3d chipsets), about 95% of the time is spent in tight loops.

Once the algorithm itself cannot be pushed further, the trick is to identify where these frequently-executed loops are, and rewrite (the inner part of) them in assembly.

Now, once you optimized the heck out of these parts, you can afford to lose 0.2% for moving your code into C++ (why not?) nicely-organized classes, methods and improving the design altogether.

In other words: when the inner part of a loop (which is called, say, 10 million times per second) is optimized, one shouldn't worry about losing 30 nanoseconds for a function call which occurs once every second 

PS:  regarding the original question, I don't think how C++ could be useful for algorithmic research.

How much useful is C++ in algorithm research?

Reply #9
Quote
I was wondering... How come standard C programming language is used in algorithm research instead of C++ with all its fanciful classes and encapsulations?
How come the MPEG standardization committee release a source code based on standard C instead of C++ ? Of course, I also noticed that  most algorithm researchers actually prefer standard C instead of C++?

I can't really speak for anyone but myself, but I think the reasons why C is used is it's simplicity. I find that many of the features C++ delivers aren't really needed. C++ is a huge language, and it's not even strictly object oriented. If I wanted a good object oriented language, I'd probably be using Java or Objective-C, and if I want a language for writing complex math, C introduces virtually no overhead. Plus, there are many portability issues with C++, and even version conflicts due to name mangling. C++ code compile with GCC 2.95, 3.1 and 3.3 aren't binary compatible, for instance.

Properly written C code works everwhere. For things like math, it allows you to do complex bit fiddling. C's main drawback is that it's highly error prone, but I hear that C++ doesn't quite solve that. With C, a good math algorithm can be implemented in the fastest possible way. There is virtually no overhead; C is just fancy assembler. I believe it's a huge misunderstanding that C++ replaces C. Heck, the C++ language isn't even a direct superset of the C language.

I dislike C++ for the exact same reasons why Trickos likes it. To me, a language is nothing more than a tool, and the language should not have too many syntactic additions. I find something like streams completely redundant; if I want to write to output, object orientation works great in OO languages like Java, and procedures work great in C - no need to add the extra syntactic sugar. I find the best argument against C++ the size of books about it. Can anyone really write 1000 interesting pages about one single language? Simplicity is a virtue - just pick a design philosophy and follow it. IMO the main alternatives to C for algorithms are functional languages. I've just begun learning ML, and I like how something like recursiveness is handled.

Languages are tools, nothing more, nothing less. Pick the one which suits your needs best; they all have their advantages and drawbacks.

How much useful is C++ in algorithm research?

Reply #10
danchr: I agree with what you said about C efficiency, simplicity and availability, which explains why it is kept for most of computational libraries (audio/video codecs). I really think that the definitive argument is support for low-end hardware, which is the main target of codecs in the end.

However I disagree with the following statements :

Quote
Plus, there are many portability issues with C++, and even version conflicts due to name mangling. C++ code compile with GCC 2.95, 3.1 and 3.3 aren't binary compatible, for instance.


These are GCC issues. Static and dynamic library formats output by MSVC and windows compilers in general have been stable for years, and this is likely to be the same with other compiler vendors.

Quote
Properly written C code works everwhere.


The same for C++ . But I understand, you were talking about C compiler availability (especially on specific, low end, hardware platforms).

Quote
I find something like streams completely redundant; if I want to write to output, object orientation works great in OO languages like Java, and procedures work great in C - no need to add the extra syntactic sugar


C++ streams are actually classes which probably work like the Java ones (I don't know Java enough to be sure about that). The fact is their interface is a bit obfuscated, because nobody is perfect, and because these streams are highly customizable (output layer, formatting layer, etc...). And I prefer them to C printf-like functions with varargs and type-unsafe arguments even if the latter  happens to be faster.

Quote
Simplicity is a virtue - just pick a design philosophy and follow it.


Which is probably the main reason for C++ to exist : pragmatism. I don't want to start useless polemics about Java speed, interpreted vs compiled and such, but, there are many applications which requires multiple programming paradigms. Not everything can be (efficiently) coded in Lisp or scales well in C. There are two solutions to this:
- Use a specialized language in every specialized area. That's a beautiful idea however merging this kind of code is problematic.
- Use a multi-paradigm monster : C++.

I do not advocate for any of these solutions, I just happened to learn C++ in the first place. The current trend, with MS .Net for example, seems to focus on language interoperability to ease the first solution. However, it is not done yet, and I am not sure it is actually more simple than C++.

How much useful is C++ in algorithm research?

Reply #11
Quote
- Use a multi-paradigm monster : C++.

In which way would you call C++ multi-paradigm? I just call it a monster...

How much useful is C++ in algorithm research?

Reply #12
Quote
These are GCC issues. Static and dynamic library formats output by MSVC and windows compilers in general have been stable for years, and this is likely to be the same with other compiler vendors.

I second that, but there are other portability issues in MSVC. Specific extensions and broken code allowed to compile are the first problem. Then if you do consider time passing, crazy dependencies of big projects with version-dependant MFC functions, .NET hassles and a different DLL hell for each version of windows become a big drawback.

System-dependant software must be well-written, otherwise it can be dangerous for the vendor (e.g. Easy CD Creator which was basically rewritten to work on winxp !!)

One thing's for sure, the only portable win32 program that can be recompiled in 5 years, is a console-based os-independant C++ project with no resource files and no system calls.

In other words.. a true algorithm without bells and whistles 

Any software should contain lots of algorithms and very simple I/O..

How much useful is C++ in algorithm research?

Reply #13
ErikS:
Quote
In which way would you call C++ multi-paradigm? I just call it a monster...


Some quick citations from the AT&T glossary

Quote
multi-paradigm programming - programming applying different styles of programming, such as object-oriented programming and generic programming where they are most appropriate. In particular, programming using combinations of different programming styles (paradigms) to express code more clearly than is possible using only one style

C++ - a general-purpose programming language with a bias towards systems programming that supports procedural programming, data abstraction, object-oriented programming, and generic programming. C++ was designed and originally implemented by Bjarne Stroustrup.


But as Stroustrup said in another interview:

Quote
Bjarne Stroustrup: I think that "paradigm" is an overused word, and I prefer the less pretentious "programming style".


NumLOCK:
Quote
System-dependant software must be well-written, otherwise it can be dangerous for the vendor (e.g. Easy CD Creator which was basically rewritten to work on winxp !!)


Do not talk about that, I finally clean the whole Easy CD Creator mess two hours ago (disappearing drives, random freezes...)

How much useful is C++ in algorithm research?

Reply #14
I think a big reason why a lot of "reference algorithms" are written in C is because it's easy to insert them into either a C++ or C program.  If the reference is in C++ it can be difficult to put them into a project that's already implemented in C.

But for what it's worth, I think statically typed languages are going to eventually go the way of the dinosaur anyway.  :-)
I am *expanding!*  It is so much *squishy* to *smell* you!  *Campers* are the best!  I have *anticipation* and then what?  Better parties in *the middle* for sure.
http://www.phong.org/

How much useful is C++ in algorithm research?

Reply #15
Quote
NumLOCK:
Quote
System-dependant software must be well-written, otherwise it can be dangerous for the vendor (e.g. Easy CD Creator which was basically rewritten to work on winxp !!)


Do not talk about that, I finally clean the whole Easy CD Creator mess two hours ago (disappearing drives, random freezes...) 

I know what you mean.. glad you could sort that out 

Well at least, *they* had an excuse to rewrite the code (ie: it *had* to work on xp kernel).

On the other hand, take Netscape for example..  they decided to rewrite version 4.0 from scratch..  what a completely stupid idea, which made them waste 1 1/2 years of development ! Needless to say, meanwhile the field was free for Microsoft. And those guys rarely think once (let alone twice) when there's fresh meat to eat 

I know, I didn't like Netscape either, but that's not the point 

About the topic, I think experience, not the programming language, is the key.

I can do crappy, slow programs in C, Java, Assembly, (...) any day 
More interesting stuff mostly comes in experience. And real ideas rarely need a specific language to express themselves. ©

Regards

How much useful is C++ in algorithm research?

Reply #16
Quote
I think a big reason why a lot of "reference algorithms" are written in C is because it's easy to insert them into either a C++ or C program.  If the reference is in C++ it can be difficult to put them into a project that's already implemented in C.

Indeed.

I see, however,  other important reasons:
- more than 80% of the today's code was written is C (IIRC)
- any deterministic algorithm can be expressed in C, and...
- C is often one of the shortest ways to express it.

Quote
But for what it's worth, I think statically typed languages are going to eventually go the way of the dinosaur anyway.  :-)

I don't know about you, but I personally would far prefer being called a dinosaur for the rest of my life, than starting to use Variant types in Visual Basic ! 

For historical accuracy, I must confess that I first learnt programming in QuickBasic ™.  No it didn't have the infamous variant type, and yes, it provided subroutines and was free of "line number" & "goto" practices 

I think Java goes the right way with garbage collection, but again, the faster algorithms will always deal with primitive types (thus needing no one to tidy up their mess)..

One good thing is no-compromise portability.  One bad thing is people don't get bitten with sharping-edge pointer problems, caused by their inappropriate thinking.

Void pointer is not the answer  B)

How much useful is C++ in algorithm research?

Reply #17
Quote
I see.. without objects right? Does that mean that these routines should be place within the same class?

Okay, I oversimplified the matters a bit. I was just trying to express that it's possible to write very C-stylish code with C-stylish performance in C++. There is absolutely no reason to ditch OOP completely in C++. The overhead of C++ depends greatly on the structure of your program and the style of your source code. You need to know how different constructs perform and when is the appropriate time to use them. For instance;

- object construction can be slow
- allocating memory from the heap is slow (as in C)
- calling virtual functions is slow, especially with complex class hierarchies
- accessing data or functions through pointers is can be slow (as in C)

It's not that you couldn't use these language constructs; you could and probably should. It just might be best to avoid using them in any performance-critical part of the code, such as in a tight calculation loop.

As for using OOP, go nuts. Create just as many classes as you need. As NumLOCK said, a tight loop in the code could run for 10 hours or so; it doesn't matter if it took 40 nanoseconds longer to start the loop by executing a virtual function instead of a non-virtual one.

How much useful is C++ in algorithm research?

Reply #18
Quote
I don't know about you, but I personally would far prefer being called a dinosaur for the rest of my life, than starting to use Variant types in Visual Basic! :phear:

I'd rather have toothpicks soaked in sulfuric acid shoved under my fingernails than ever use Visual Basic at all.  I'd much rather have something like Perl or Python.
I am *expanding!*  It is so much *squishy* to *smell* you!  *Campers* are the best!  I have *anticipation* and then what?  Better parties in *the middle* for sure.
http://www.phong.org/

How much useful is C++ in algorithm research?

Reply #19
GCC had some issues with compatibility, but it is now gone and will be stable, because it's standard now.
I hope it will be possible to compile foobar2000 plugins with it some day.

My problems with MSVC++ 6 SP 5:
- non-compliant C++ (notorious hacks to support it)
- problems with simple code (check my thread at foobar2000 Developer section)

Another quite good compiler is Borland C++ (not free). I'll give it a run with foobar plugins.
ruxvilti'a

How much useful is C++ in algorithm research?

Reply #20
Quote
Another quite good compiler is Borland C++ (not free).

It's also one of the slowest C compilers ever, I reckon.

Code generated with it is rock stable (I.E, doesn't feature some of the instabilities introduced by ICL), but still, runs amazingly slow. I once saw some code that was faster when generated in MSVC debug mode than when generated in BCC release mode. :B

At least, it compiles very quickly...
http://www.willus.com/ccomp_benchmark.shtml?p10

How much useful is C++ in algorithm research?

Reply #21
I think that Fortran and C, probably Algol 66 & Pascal there are languages to express algorithms. C++ and Modula 2 are for express systems. Most algorithms are published in pseudo code anyway.

How much useful is C++ in algorithm research?

Reply #22
Quote
Quote
Another quite good compiler is Borland C++ (not free).

It's also one of the slowest C compilers ever, I reckon.

Code generated with it is rock stable (I.E, doesn't feature some of the instabilities introduced by ICL), but still, runs amazingly slow. I once saw some code that was faster when generated in MSVC debug mode than when generated in BCC release mode. :B

At least, it compiles very quickly...
http://www.willus.com/ccomp_benchmark.shtml?p10


I think that was some years ago.. I too noticed that.. I did a comparison between codes compiled by BC 3.1 and VC 5.0 and VC 5.0 outperforms BC by many times.. However, my superiors pointed out that VC 5.0 was actually taking advantage of the "improved" floating point  performance of the Pentium processor..

That was an old story.. but I am not very sure of the latest Borland Compilers..


wkwai

How much useful is C++ in algorithm research?

Reply #23
Quote
You need to know how different constructs perform and when is the appropriate time to use them. For instance;

- object construction can be slow
- allocating memory from the heap is slow (as in C)
- calling virtual functions is slow, especially with complex class hierarchies
- accessing data or functions through pointers is can be slow (as in C)

It's not that you couldn't use these language constructs; you could and probably should. It just might be best to avoid using them in any performance-critical part of the code, such as in a tight calculation loop.


Such flexibility is possible with C++ but for other programming languages such as Java, OOP approach is a must.. I implemented a FFT in Java sometime ago and its performance is incredibly slow! There is just no way to completely avoid using Java's language constructs..

How much useful is C++ in algorithm research?

Reply #24
Quote
I think that was some years ago.. I too noticed that.. I did a comparison between codes compiled by BC 3.1 and VC 5.0 and VC 5.0 outperforms BC by many times.. However, my superiors pointed out that VC 5.0 was actually taking advantage of the "improved" floating point  performance of the Pentium processor..

That was an old story.. but I am not very sure of the latest Borland Compilers..

That comparision is using the freely available BCC 5.5.1. I don't know if they have more up-to-date compilers.

And, as you can notice by the flags used, that benchmark is using all optimizations possible! :B