Wednesday, December 08, 2004

Re: which is faster : C or C++?

which is faster : C or C++?

I think it would a good idea to first define "faster". What exactly do you mean? Faster in what context? In a one line program or in a 100,000 line program? And how do you analyse the performance?

Personally, I feel it's an exercise in futility to compare what language is "faster" than the other. The reason I feel that way is cause you'll find studies and papers claiming that each language can beat every other one.

Use of virtual functions and run-time polymorphism slows down the code a little. So if this feature of C++ is not used, C++ code would run as fast as C code.

I think this is a wrong approach with which to look at C++. C++ was created to be a "better" C. You can take that to mean whatever you want (everyone has their own opinion about why it's better (if at all)). From what I understand, as applications started getting larger, using C to develop them was getting to be a pain in the arse. They needed something that would make it easier to write maintainable code (Isn't code always easier to write than read?). Enter C++. It created another level of abstraction, just as C created an abstraction over assembly, and assembly over machine code, and machine code over gates, and gates over the 0s and 1s, and the 0s and 1s over the electrons... you get the picture (did I miss a level?).

Anyway, my point (yeah I have one!) is that if you look at C++ feature by feature and look to eliminate something so as to get it to run as "fast" as C... you might as well cut to the chase and go play with electrons.

Having said this, the difference between run times is due to the compilers and not the languages themselves. Last I heard, C++ compilers don't optimize C++ code as well as C compilers optimize C code.

Did you know that the first C++ compiler (CFront) generated C code... not machine code? So any optimization made to C compilers would apply to C++ code as well. Today, every C++ compiler most likely generates native code, but I don't see any reason why they would be any less optimizing than C compilers.

Again the abstractions bit comes in. It's all about the amount of control you (the programer) want to have. You can write programs with 0s and 1s if you want to... you got all the control in the world. I wouldn't imagine it would be very fun to do it, but you can if you want to. You sure as hell won't be very productive. Just as you lost some control when you went from C to C++ (like creating/destroying objects, does multiple things... you don't have control over the entire process), going from C++ to Java/C# you lose even more control. But what you gain is productivity.

The code that goes into CFD applications handles millions of points, so even a little function overhead (eg virtual function) is significant.

I read this on some (smart) dude's blog about performance: "Always set goals and always measure". What's good enough for you? If you code the app in C++ and it's slower than it was with C, but good enough then does it matter? Depending on how good you are with each language you might be a lot more productive with C++. So it's a tradeoff.

Bottomline - if virtual functions, run-time polymorphism isn't used, C++ code would run as fast as C code.

There are a lot more abstractions than just virtual functions in C++, so I doubt if you just avoid that if it would make a huge difference.

For starters, about me -- I have no clue to Java; I'm pretty good at C on UNIX/Linux etc and I can bear C++.

Firstly, cheers on your first post. Hope to see a lot more.

Just to give a brief intro to what we dudes are about...
Hrishi - (you probably know more) C/Linux
Rahul - Java/Linux
Dinesh - C++/AI/Game engines/Philosophy
Yours truely - C#/.NET/Bit of Java/Bit of C++

PS: Did you guys know we guys here call Hrishikesh, "Micro"? Micro?! Huh! near Mega you'd say... well... but then it all started from a Micro-elephant :D

Dyou see the archive links on the right hand side of the page? Go to November 2003 and check the very first post's title and ask Micro to explain it. Post your reaction.

No comments: