Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am refactoring code which contains many lines of 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?

share|improve this question

migrated from stackoverflow.com Nov 29 '11 at 21:28

This question came from our site for professional and enthusiast programmers.

2  
Have you tried profiling them? Chances are the difference isn't enough to notice. That said, this seems to be about improving working code, so it'd be better on codereview.SE. – Kevin Nov 29 '11 at 20:43
1  
It would be easy to test this yourself with a couple of loops and some logging statements. – jrturton Nov 29 '11 at 20:44
    
Related: stackoverflow.com/questions/8275131/… – Josh Caswell Nov 29 '11 at 21:07

2 Answers 2

up vote 4 down vote accepted

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:

  • 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.

share|improve this answer
    
Is every call to append* really going to cause an allocation? stackoverflow.com/a/3730437/37020 – orip Mar 10 '14 at 15:49

You should benchmark the two methods to see which is faster. Example:

double startTime1 = CACurrentMediaTime() / CLOCKS_PER_SEC;

NSString* testString1 = [@"fragmentOne" stringByAppendingString: @"fragmentTwo"];

double endTime1 = CACurrentMediaTime() / CLOCKS_PER_SEC;


double startTime2 = CACurrentMediaTime() / CLOCKS_PER_SEC;

NSString* testString2 = [NSString stringWithFormat: @"fragmentOne%@", @"fragmentTwo"];

double endTime2 = CACurrentMediaTime() / CLOCKS_PER_SEC;


double diff1 = (endTime1 - startTime1) * 1000000000000000;
double diff2 = (endTime2 - startTime2) * 1000000000000000;

NSLog(@"stringByAppendingString time: %f", diff1);
NSLog(@"stringWithFormat time: %f", diff2);

I probably could have just run the test myself in the time it took to write this, but whatever, now you know how to benchmark methods :) If it were me, I'd use stringWithFormat because it tends to make for more human-readable code.

Hope this helps!

share|improve this answer
    
@Ian, thanks for the snippet! – OnlineCop Nov 29 '11 at 23:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.