Re: string literals and performance
Re: string literals and performance
- Subject: Re: string literals and performance
- From: "Stephen J. Butler" <email@hidden>
- Date: Sat, 24 May 2014 23:18:38 -0500
You misunderstand the point of the Stackoverflow answer. In the first
example you've given it looks like "s" is just a stack local variable. In
that case there is no difference between the two examples. Actually, in the
face of optimization I bet the assembly turns out exactly the same. Use the
later because it is more clear what's going on.
If you did use the same string literal in multiple .m files then you might
get a tiny performance benefit from the "extern NSString* const". But I'd
argue that the real benefit to that is if you ever decide to change the
value then you don't have to worry about updating multiple locations and/or
projects (in the case of a framework).
On Sat, May 24, 2014 at 11:08 PM, 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"];
>
> 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. 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.
> 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?
>
>
> Thanks for your thoughts.
>
> P
>
> _______________________________________________
>
> 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
>
_______________________________________________
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