Re: Program Efficiency
Re: Program Efficiency
- Subject: Re: Program Efficiency
- From: Peter Ammon <email@hidden>
- Date: Wed, 06 Jun 2001 09:54:52 -0700
on 6/5/01 8:03 PM, Erik Thorteran at email@hidden wrote:
>
Which of the following snippets of code will execute faster?
>
Snippet one:
>
>
[[NSDictionary dictionaryWithObjectsAndKeys:[[NSBundle mainBundle]
>
bundlePath],@"path,etc, etc.] writeToFile:@"Wherever" atomically:YES]
>
>
Snippet two:
>
>
NSMutableDictionary *dict;
>
dict=[[NSMutableDictionary alloc] init];
>
[dict setObject:[[NSBundle mainBundle] bundlePath] forKey:@"path"];
>
[dict setObject:continue forKey:@"irrelevant"];
>
etc. etc.
>
[dict writeToFile:@"Wherever" atomically:YES];
>
>
Basically, is it more efficient to pack everything into one line, or
>
multiple lines, or dies it not matter?
>
>
Erik Thorteran
In general, immutable objects are faster than mutable ones and require less
memory, so I'd expect the first snippet to be slightly faster than the
second. If this is in a performance sensitive part of your code, then you
might get even better performance using the
dictionaryWithObjects:forKeys:count: method. Of course, you should profile,
since premature optimization is the root of all evil ;)
-Peter