Software:
News ToolsReg Shops |
Don't forget the ‘C’ in Objective-CRuntime efficiency issues in Mac Cocoa programmingPublished Friday 6th April 2007 13:02 GMT Part One Nowadays, it’s all too easy to take today’s fast processors for granted. At the risk of sounding like an old fogey, I get the impression that a lot of developers do just that. This devil-may-care attitude is not, in my opinion, the result of complacency but far more likely due to inexperience or even – dare I say it? – ignorance. Just as a good driver has a certain understanding of, and empathy with, the mass of rotating, reciprocating hardware a few feet in front of him, I reckon the best programmers have some ‘feel’ for the way in which their code is mapped onto machine code instructions; an understanding of the demands they’re making on the processor, if you will. I’ve felt this way for some time, but the point was recently hammered home while I was browsing around inside the internals of a certain Mac OS X utility – as one does. The programmer in question wanted to use OpenGL effects in his application, and in order to do that, he needed to establish the capabilities of the installed OpenGL system. In particular, the developer wanted to check for the presence of the The conventional way of doing this is to make the following call: char *extensions = glGetString(GL_EXTENSIONS); This will give you a plain-vanilla C string containing a list of all the available extensions. Having got hold of this string, here’s what our anonymous Cocoa developer did; firstly, he used the OK – I’m not disputing the fact that this code works, but to me, it seems a really roundabout way of doing things. For starters, a more experienced Mac programmer would simply have used the Cutting to the chase…Sure there is. As the title of this article points out, Objective-C is built on top of C, and the C runtime language has a perfectly good, tried and trusted runtime library routine for finding one string within another! All of the above convoluted shenanigans can be replaced with this:
BOOL gotTexturedRects
= strstr(glGetString(GL_EXTENSIONS), "GL_EXT_texture_rectangle")
!= nil;
I guarantee that the above statement will execute far more quickly than the aforementioned alternative. Why? Because this code involves exactly two native subroutine calls while the Cocoa alternative makes an assortment of calls, both inside and outside the Cocoa frameworks, and of course each time you use a Cocoa message, the Objective-C dispatcher is invoked to figure out which actual routine to call. To be clear: I’m not knocking Cocoa or Objective-C here. All I’m saying is that a little familiarity with the underlying C runtime library can translate into big dividends in performance terms. Determining the capabilities of the OpenGL system is something that you’ll typically do once only, in the initialisation part of your program. But looking for one string inside another is something that you might do tens of thousands of times inside a text crunching application. Using As a general rule, if you’re working with an API which expects/returns C-style strings (such as OpenGL) then it’s often much quicker to use a standard C library routine than it is to convert the string into an
NSString *str = [NSString stringWithCString: myCString
encoding: [NSString defaultCStringEncoding]];
int len = [str length];
You might think I’m exaggerating, but I have seen code that does this. My friend, let me introduce you to Again, don’t get me wrong: Next time round, we’ll be looking at other Cocoa programming issues from a runtime efficiency perspective, and I’ll be looking at ways in which Cocoa applications can be structured more effectively. 43 comments posted — Comment period finished Bad ExamplePosted: 13:45 6th April 2007 No, C really is dead.Posted: 13:55 6th April 2007 Further flawed examplePosted: 14:54 6th April 2007 RE: Bad ExamplePosted: 15:30 6th April 2007 Speed?Posted: 15:41 6th April 2007
Track this type of story as a custom Atom/RSS feed or by email.
|
Developer HeadlinesThe UK's latest developer news from MSDN |
Top 20 stories • All The Week’s Headlines • Archive • Search