I need to convert a lot of long long's to NSString. What is the fastest way to do this? I am aware of two ways to do this and I am wondering if there is anything else that would be faster.
NSString* str = [NSString stringWithFormat:@"%lld", val];
And
NSString* str = [[NSNumber numberWithLongLong:val] stringValue];
where val is a long long (64 bit int). The first method has the small overhead of parsing the string and the second has the overhead of allocating an additional object. Most likely NSNumber uses NSString's stringWithFormat, but I am not sure.
Does anyone know of a faster method for this?
I put together a basic profiling app. I tried your two approaches, and Rob Mayoff's two approaches.
stringWithFormat:
took an average of 0.000008 seconds. The other three approaches (callingstringValue
onnumberWithLongLong:
), and Rob's two, all took an average of 0.000011 seconds.Here's my code. It's obviously not 100% accurate since there are a few other operations included in the profile, but the discrepancies will be the same in all of the tests:
This is the fastest to type:
This requires one additional keystroke:
If you mean the fastest at run time, the only way to be sure is to try it both ways and see. Profile. Don't speculate.