Re: ObjC in time-critical parts of the code
Re: ObjC in time-critical parts of the code
- Subject: Re: ObjC in time-critical parts of the code
- From: Michael Ash <email@hidden>
- Date: Thu, 15 Jan 2009 18:18:54 -0500
On Thu, Jan 15, 2009 at 5:16 PM, Jens Bauer <email@hidden> wrote:
> Hi all,
>
> I just want to let you know that I discovered I did a terrible mistake
> today.
> In other words: you don't have to learn from your own mistakes, if you can
> learn from mine. =)
>
> I investigated this, because my rendering was choppy.
>
> The code...
>
> - (void)renderObject
> {
> }
>
> - (void)renderAll
> {
> UnsignedWide us;
> double time;
>
> Microseconds(&us);
> time = ((double) us.hi) * 65536.0 * 65536.0 + ((double) us.lo);
> [self renderObject];
>
> Microseconds(&us);
> time = ((double) us.hi) * 65536.0 * 65536.0 + ((double) us.lo) -
> time;
>
> NSLog(@"time:%.3fms", time * 0.001);
> }
>
> ..often used around 3ms for the empty method "-renderObject".
> It gave me the terrible result of up to 21ms spent in the empty method!
If you're measuring an average of 3ms for an empty message send then
you're doing something really wrong. On a modern Mac you might see an
average of 5 *nano*seconds to call an empty ObjC method. The ObjC
messaging system has an overhead of perhaps a dozen CPU cycles per
invocation. It is utterly negligible in this sort of situation, and in
fact it will be essentially impossible to measure a single invocation.
The only good way to measure it is to perform at least several million
of them in a loop, then measure the total time.
Every programmer should have an idea of how long various typical
operations take to run, which is why I built this list:
http://www.mikeash.com/?page=pyblog/performance-comparisons-of-common-operations-leopard-edition.html
Without knowing more, it's hard to say *what* is going wrong for you,
but something definitely is, as your numbers are about a million times
too large. Maybe your method isn't as empty as you think, or maybe
your measurement isn't actually producing the correct values.
> -So I'd like to let you know that it's sometimes good to think "do I really
> need this method?"
> I will be rewriting around 8 of my large files for doing some rendering, so
> they will be using C-routines instead of ObjC methods, since I'm using them
> in a real-time environment.
Really, don't do this. The overhead of ObjC is important in some
situations, but it's not *that* important.
> My guess is that the message dispatcher probably needs to do something else
> than servicing *my* application, so I believe it's the nature of the
> environment, not a bug.
This doesn't really make sense. The dispatcher lives in libobjc which
in turn gets loaded into each process individually. You aren't sharing
your dispatcher with any other processes. The dispatcher is just a
plain old C function (actually written in assembly, but close enough)
that gets called by your code and which in turn calls the
implementation of the correct method, it's nothing magical.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden