 |
 |
Hardcore Optimization Question
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
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
|
|
</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<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<[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
|
|
</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<[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
|
|
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>=0; i--)
{
//do your thang
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">But that's just me.
F-bacher
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|