Re: string literals and performance
Re: string literals and performance
- Subject: Re: string literals and performance
- From: Quincey Morris <email@hidden>
- Date: Sat, 24 May 2014 21:28:57 -0700
On May 24, 2014, at 21:08 , 2551 <email@hidden> wrote:
> Are there any performance implications that would suggest preferring one or the other of these different styles?
>
> NSString *s = @"sing me a song";
> [myClass aMethod: s];
>
> and
>
> [myClass aMethod: @"sing me a song”];
Basically — if not literally — no.
> I have a lot of the first kind in my code, and I'm thinking of simplifying it by turning them all into the second kind.
There’s a good chance that the compiler already optimizes the first form into the second form.
> A discussion on stackoverflow here:
>
> http://stackoverflow.com/questions/25746/whats-the-difference-between-a-string-constant-and-a-string-literal
>
> seems to suggest (if I've understood it correctly) that I'd be better off sticking with the first style to avoid errors and to speed up comparisons.
There is no difference in performance that you should consider *unless* you have documented evidence of certain string comparisons causing performance problem. Any other stance is unnecessary premature optimization (and you don’t know that it will make any kind of difference at all).
The case discussed on SO where you use a global string variable is also unnecessary premature optimization, again unless you have some reason to believe you have a problem.
If this really worries you, you can *try* to write a sample app where the source coding style leads to a measurable performance difference. If you can do it, post the code here, because I’m sure we’d all be flabbergasted to see it. ;)
> However, since in my code they're all one-off 'throw away" strings that don't get used elsewhere, that doesn't seem relevant. Is there any other reason why I should stick with the first style rather than the second?
No, the second style is better IMO:
a. It’s less keystrokes to type.
b. It’s clearer when you read the source code again later.
OTOH, if you’ve got a method invocation that is very long, it could be be argued that splitting it into multiple lines (the first style) makes it easier to read. Since it’s almost entirely a stylistic matter, use the form that looks less ugly to you.
_______________________________________________
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