Monday, January 19, 2009

C++ and Performance

Here's an interesting blog post about using ObjC++ to improve performance in iPhone apps. I may be in a minority on this, but I've never been a huge fan of the Objective-C++ language. Why straddle yourself with the limitations of two languages?

Okay, that's an over-simplification; there are times when Objective-C++ is a great choice, especially when you are writing the Mac or iPhone version of an application that runs on multiple platforms. It wouldn't however, be the first option that leapt to my mind as a tool for optimizing a poorly-performing iPhone application. Although C++ does some things, like message dispatching, faster (well, technically it's a "vtable lookup" on C++, not a message dispatch), it still incurs some overhead for those things so, it would seem to me, any optimization in C++ is going to leave some room for further improvement. Any way you cut it, though, there's definitely some really valuable stuff in that blog posting in terms of iPhone coding and performance, so go read it.

I'm in the middle of re-writing the drawing code for my particle engine to be faster and am dealing with some of the same issues this blog post talks about, however I chose a slightly different, more old-school option for my optimizations: static inline C functions and structs for the really time-sensitive stuff, and straight Objective-C for anything that isn't time sensitive. Sure it can be a pain to debug, but I only move code into an static inline function after I've had it running inside a regular method or function first. I use the structs to avoid the object creation overhead on things like particles, which there will be a lot of.

So far, it seems to be a good choice - I've had a 2,000 particle per-second generator with a 12-second particle-lifespan using GL_POINT rendering running at 20 frames per second on a first-generation iPhone without much noticeable skipping, which is a huge improvement over my first version, which skipped with even a few hundred particles per second. I'm hoping I see similar improvements in the other rendering types when I finish re-writing those.



8 comments:

Peter N Lewis said...

Personally I switch Xcode to use Objective-C++ for all my source code, simply because C++ is a better C. An added bonus is the ability to use the STL for extended basic types like lists and arrays where the Objective C equivalent would be overkill (or too slow).

There is essentially no down side in using Objective-C++ for everything (although there can be a downside in using some C++ features like templates and even objects if you don't understand them).

Jeff LaMarche said...

Peter:

I know a lot of people view C++ that way, but I don't. I feel like C++ lost its direction about a decade ago and their kitchen-sink approach to language development is inelegant and scattershot. There are times when I use it, but certainly not as a regular practice, and it probably would never occur to me to use it as an optimization. To me, Objective C++ occupies an uncomfortable middle-ground where you usually give up more than you gain.

I can code in Objective-C++. I've done an awful lot of C++ over the years and some ObjC++, but I usually choose not to unless there's a compelling reason to do it.

But a lot of people, inside Apple and out, share your view. It's certainly a valid and supportable opinion, it's just not one I share. There's probably about as much value in arguing the point as in arguing over whether vi or emacs is better. :)

Peter N Lewis said...

I'm curious as to what in particular you dislike about turning on Objective C++ and then programming as you always have.

To me, turning on C++ gives more consistent struct/enum usage and more rigid type checking and tighter controls over some of the more dangerous C constructs. C++ also offers struct copying (useful for Rects and such) function overloading (useful for a function that takes different parameters (eg const char* and NSString*).

But I'm curious as to what problems you think just turning on the C++ compiler causes...?

And believe me, I am NOT a C++ zealot - I programmed in Pascal until 2007 when I sold Interarchy!

You may well be right that there is no point in arguing about this, I'm just curious to know what you think is wrong with C++ as a variant of C as opposed to what is wrong with the various additions in C++ like classes, templates, etc, which you're free to use or not regardless of the language setting.

Joestradamus said...

A couple of things... You only have a vtable lookup if you're calling a virtual function. Non-virtual function calls should be as fast as they are in C. Also, if you absolutely need speed on some simple functions, you can inline them - which means you don't get the overhead of even a C function call (but it will generally make your program larger). I'm no fan of C++, but it is a very powerful language and it is useful for optimizing the most time/memory intensive routines...

vaillant said...

I agree with Peter and Joestradamus (however, C does have function inlining and has had it for some time now). One of the biggest reasons to use C++--that is often taken for granted--is RAII. E.g. objects are almost always created on the stack, with memory management completely encapsulated (efficiently provided by stdlib containers for that matter), and often without sacrificing polymorphism. As Stroustroup puts it "Clearly, if your code has new operations, delete operations, and pointer arithmetic all over the place, you are going to mess up somewhere and get leaks, stray pointers, etc. This is true independently of how conscientious you are with your allocations: eventually the complexity of the code will overcome the time and effort you can afford. "

Cheers,
Marc

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

Tharik Sham : Mobile Tricks said...

Nice Post

pcmobileshelper.blogspot.com
Pc Mobile Help and Mobile Tricks


mobiletrickspc.co.cc
Mobile-Tricks Home

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels