Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Hardcore Optimization Question

Hardcore Optimization Question
Thread Tools
Forum Regular
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jun 18, 2002, 02:20 AM
 
Hi,

I have some questions to optimize code execution and memory :

First, what is the difference, for the cc compiler (and the coming gcc 3.1) between this :
#define kMyStringConstant @"Hello, I am a constant"

and this :
static NSString *kMyStringConstant = @"Hello, I am a constant";

What is the most efficient in a Cocoa project ?

Yet, another question :
What is the best, for an NSArray, between using an NSEnumerator, and this :
int loop, max;
max = [myArray count];
for (loop=0; loop<max; loop++)
{
do something with [myArray objectAtIndex:loop]
}


And if I use this instead :
for (loop=0; loop<[myArray count]; loop++)
Will the compiler be able to optimize the [myArray count] access, or will it go into myArray and extract count at each iteration ?

Thanks
Imagine that my signature is here...
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jun 18, 2002, 09:35 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by hELLO wORLD:
<strong>Hi,

I have some questions to optimize code execution and memory :

First, what is the difference, for the cc compiler (and the coming gcc 3.1) between this :
#define kMyStringConstant @"Hello, I am a constant"

and this :
static NSString *kMyStringConstant = @"Hello, I am a constant";
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">The former is equivalent to [NSString stringWithCString:"Hello, I am a constant"], and is evaluated each time it is used. The latter is assigned only once, and so is more efficient in terms of cycles - however, if you're using it in a global context, it's going to take up more memory. More efficient, for NSStrings, is probably [b].

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>
Yet, another question :
What is the best, for an NSArray, between using an NSEnumerator, and this :
int loop, max;
max = [myArray count];
for (loop=0; loop&lt;max; loop++)
{
do something with [myArray objectAtIndex:loop]
}

</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">An NSEnumerator is definitely better. Because an NSArray is actually a collection, not a true array, underneath, objectAtIndex can be quite expensive.

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>
And if I use this instead :
for (loop=0; loop&lt;[myArray count]; loop++)
Will the compiler be able to optimize the [myArray count] access, or will it go into myArray and extract count at each iteration ?

Thanks</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">cc (which is gcc, by the way) does optimize such things, I _think_. However, the only way to be sure would be to have a look at the assembler it generates Or read some documentation somewhere...
[vash:~] banana% killall killall
Terminated
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jun 18, 2002, 03:32 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Gul Banana:
<strong>
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>
And if I use this instead :
for (loop=0; loop&lt;[myArray count]; loop++)
Will the compiler be able to optimize the [myArray count] access, or will it go into myArray and extract count at each iteration ?

Thanks</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">cc (which is gcc, by the way) does optimize such things, I _think_. However, the only way to be sure would be to have a look at the assembler it generates Or read some documentation somewhere...</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I don't think this is correct. If the number of elements in the array changes inside of the loop, the loop will still work correctly. So, it's calling [myArray count] every iteration.

More specifically, the second "phrase" in the for loop is evaluated completely every iteration. You can use "const" for optimization, but as written, it'll evaluate.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jun 19, 2002, 11:13 AM
 
count is a method which I assume returns an instance variable of NSArray which doesn't change if we're talking about NSArray only, and does change only on creationg and when adding/removing objects if we're talking about NSMutableArray. But I'm just assuming. It would be highly inefficient if it recalculated the size every time. If possible, I think this would be better (if you're not going to use an enumerator):

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">int i;

for(i=[array count]-1; i&gt;=0; i--)
{
//do your thang
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">But that's just me.

F-bacher
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2