views:

22

answers:

2

I have a UILabel in an interactive calculator application that is continually being refreshed with a newly formatted message as the user changes a UISlider value. My question is what is the most efficient way to manage the strings:

NSString *data = [[NSString *alloc] initWithFormat:@"Value A: %0.1f, Value B: %0.1f, valueA,valueB];
myUILabel.text = data;
[data release];

or

[myMutableString setString:@""];
[myMutableString appendFormat:@"Value A: %0.1f, Value B: %0.1f, valueA,valueB];
myUILabel.text = myMutableString;

Any advice would be appreciated

A: 

As the string is there to be read by the user and so has to remain visible for a time I doubt that the speed of the format matters as it will take much less time than it takes for a user to read the value.

Mark
I was thinking of efficiency from a memory management point of view
Bruce Alport
A: 

Creating a single, non-mutable string (your first example) is going to be more efficient than creating and then modifying a mutable string.

However the real answer is that it doesn't matter. The amount of time is negligible in user interaction terms, and any temporary objects created as a side effect will be freed on every pass through the event loop anyway, so worrying about this at all is premature optimization.

smorgan
Thanks for the commentsThe mutable string would be a class instance variable created only once. Perhaps as you say I am worrying too much. I blame Instruments :- I ran my code through it and saw lots of allocations, so thought re-using the variables might be more efficient.
Bruce Alport