Re: Performance, Efficiency - Coding practice
Re: Performance, Efficiency - Coding practice
- Subject: Re: Performance, Efficiency - Coding practice
- From: Shawn Erickson <email@hidden>
- Date: Thu, 28 May 2009 17:44:14 -0700
On Thu, May 28, 2009 at 4:57 PM, John Ku <email@hidden> wrote:
> I have this original class with the following method:
> - (void) update {
> NSString *title = [[NSString alloc] init];
You create an empty string instance in the above and make title point at it.
> title = @"TEST";
You then make title point a the NSString constant "TEST" blowing away
your pointer to the empty string you created above.
If you are using GC then why bother with creating the empty string? If
you aren't using GC then you are leaking the empty string instance
(granted likely only one of those would ever exist given the
implementation but...).
> NSPoint drawAtOrigin;
> drawAtOrigin.x = 0;
> drawAtOrigin.y = 10;
Consider using NSMakePoint.
> [title drawAtPoint: drawAtOrigin withAttributes: nil];
> }
>
>
> Here, I updated it trying to be efficient:
>
...snip...
> Is this a more efficient way to code?
No not likely
> Which coding practice is better in terms of efficiency, memory, performance?
Ignoring the unneeded creation of an empty string in your first
example no difference really exits, well... actually your "efficient"
example uses more memory and has less theoretical performance.
First example (omitting the unneeded empty string creation)...
- (void) update {
NSString* title = @"TEST"; << string constant compiled into
application binary
NSPoint origin = { 0.0, 10.0 }; << point struct compiled into
application binary
[title drawAtPoint:origin withAttributes:nil];
}
Second example...
- (id) init {
title = @"TEST"; << string constant compiled into application binary
origin = { 0.0, 10.0 }; << point struct compiled into application binary
}
- (void) update {
[title drawAtPoint:origin withAttributes:nil];
}
... sure it looks like less it taking place in update however title
and origin are now instance vars of an object allocated on the heap.
That takes up space in the heap (granted infinitesimally small amount
of memory compared to everything else likely in your application) in
addition to the space that are exists in your compiled binary for the
string constant and initial values for origin. Additionally in order
to send the message drawAtPoint the title pointer and origin struct
need to be loaded from the heap instead of being more "immediate" on
the stack.
> The update method will get called often.
Stepping back a little... drawing should normally only be done under
drawRect: for NSView subclasses and by under I mean in the
implementation of drawRect: or in a method it calls. Anyway make sure
you understand the Cocoa view/drawing system before going to far with
this.
> Your thoughts?
Write good clean code with well designed algorithms (yet simple as
possible) and then profile it under real-world situations before
attempting performance optimizations.
-Shawn
_______________________________________________
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