principleMinor
stringByAppendingString: vs. stringWithFormat:
Viewed 0 times
stringbyappendingstringstringwithformatstackoverflow
Problem
I am refactoring code which contains many lines of
or
In the first, you take one
The result is the same, but for optimization purposes, which is better?
For the above example, I would expect that a simple concatenation would be faster than having it parse the string for '%' symbols, find a matching type (
However, what happens when we have (sloppily written?) code which contains multiple
Here, a single
Thoughts? Sites/blogs that you've found that helps answer this?
NSString* image = [@"ipad_" stringByAppendingString:imageOrigName]; and wondered which is more optimized:stringByAppendingString:or
stringWithFormat:In the first, you take one
NSString object and concatenate another onto its tail. In the second, you can use a formatter:NSString* image = [NSString stringWithFormat:@"ipad_%@", imageOrigName];The result is the same, but for optimization purposes, which is better?
For the above example, I would expect that a simple concatenation would be faster than having it parse the string for '%' symbols, find a matching type (
NSString for the %@), and do all its background voodoo.However, what happens when we have (sloppily written?) code which contains multiple
stringByAppendingStrings?NSString* remainingStr = [NSString stringWithFormat:@"%d", remaining];
NSString* msg = "You have ";
msg = [msg stringByAppendingString:remainingStr];
msg = [msg stringByAppendingString:@" left to go!"];
if (remaining == 0)
msg = [msg stringByAppendingString:@"Excellent Job!"];
else
msg = [msg stringByAppendingString:@"Keep going!"];Here, a single
stringWithFormat (or initWithFormat if we use [NSString alloc]) would seem to be the smarter path:NSString* encouragementStr = (remaining == 0 ? @"Excellent Job!" : @"Keep going!");
NSString* msg = [NSString stringWithFormat:@"You have %d left to go! %@", remaining, encouragementStr];Thoughts? Sites/blogs that you've found that helps answer this?
Solution
The only way to know is to measure it.
And, of course, the only reason to measure it is if it actually matters; if you have a real world performance issue that can be tracked to this code.
If not, don't worry about it.
However, there are a few issues to consider:
And, finally, note that this kind of string manipulation is going to make localization more difficult.
And, of course, the only reason to measure it is if it actually matters; if you have a real world performance issue that can be tracked to this code.
If not, don't worry about it.
However, there are a few issues to consider:
- every
append*is going to cause an allocation and memory copying, those are expensive
- every
*WithFormat:is going to cause a string to be parsed (and there will be allocations/copying)
- all of this is likely entirely moot unless you are doing this 10s of thousands of times often. If you are, it begs the question as to why?
And, finally, note that this kind of string manipulation is going to make localization more difficult.
Context
StackExchange Code Review Q#6389, answer score: 4
Revisions (0)
No revisions yet.