In Objective-C, the method stringWithFormat: seems to be extremely slow and is actually a large bottleneck in one of our apps (we used the profiler to find that out).
Is there a way to optimise it or use some faster C code?
 
    
    - 3,696
- 2
- 27
- 40
 
    
    - 1,114
- 2
- 18
- 40
- 
                    11How could we know without seeing any of your code? – DrummerB Nov 26 '12 at 16:21
- 
                    I am not talking about a specific code. i am talking about the line [NSString stringWithFormat] – AJ222 Nov 26 '12 at 16:22
- 
                    How do you identify it as bottle neck of your app? – nhahtdh Nov 26 '12 at 16:24
- 
                    What do you mean by 'bottleneck'? Does it consume too much CPU? Does it make UI slow? Maybe you could relocate those calls away from main thread. – ZhangChn Nov 26 '12 at 16:25
- 
                    4Interpreting a format string is inherently slow. If you can used other techniques, and performance is an issue, then go ahead. (And, for crying out loud, don't use things like `myString = [NSString stringWithFormat:@"%@", "ABC"];` as I've seen many times here. It's totally wasted motion!) – Hot Licks Nov 26 '12 at 16:25
- 
                    3You don't use retarded code like `NSString *str = [NSString stringWithFormat:@"%@", otherString];` do you? I've seen plenty of examples of that on stackoverflow... – trojanfoe Nov 26 '12 at 16:25
- 
                    1@HotLicks great minds... – trojanfoe Nov 26 '12 at 16:27
- 
                    1(Give us two or three examples of your use of stringWithFormat.) – Hot Licks Nov 26 '12 at 16:29
- 
                    What do you mean by "use some faster C code"? Also, your question is nearly impossible to answer without code. We need to see how you are using stringWithFormat. – Stephen Melvin Nov 26 '12 at 17:04
- 
                    I simply use it for string concatanation – AJ222 Nov 27 '12 at 09:42
- 
                    @DrummerB your comment in most cases would be reasonable, but it seems that NSString stringWithFormat is *always* very slow, irrespective of the circumstances, which makes the question valid. With the help of this question, I did some experiments, and it's *always* sped up considerably by using sprintf. If you're doing any kind of rendering using strings for texture names, render path dictionaries, etc ... a 500% improvement in string concatenation speed (5x faster is typical when using sprintf) is a big thing :). – Adam Jul 02 '13 at 18:18
2 Answers
Yes
use sprintf in c http://www.cplusplus.com/reference/cstdio/sprintf/
after that push the char* in a NSString with [NSString stringWithUTF8:];
example:
char cString[255];
sprintf (cString, "%d", 36);
NSString* OCstring = [[NSString alloc] initWithUTF8String:cString];
 
    
    - 680
- 4
- 19
- 
                    Looking for a general-purpose drop-in replacement for stringWithFormat... harder than it looks! – SG1 Dec 21 '12 at 20:58
- 
                    
- 
                    @Danpe No, you don't need to release the char array, because it's not a pointer. It will be automatically released at the end of the function(if it is inside a function) or after each run of the loop(if it is inside in loop) – JMBise Oct 14 '13 at 12:24
If you're doing extensive string manipulations and operations - it sounds like you might well be doing so, and NSString really is becoming a bottleneck for your app, I recommend trying to use C++ for your string needs rather then C.
Apple admits that while NSString is great, it is top level, in fact, to make their autocorrect algorithm's for iOS they ran into a similar problem, NSString was too slow to compute and compare so many things. They then switched to C++ and got all the performance they needed.
Just a suggestion. You should definitely put up some code, I am surprised this is happening to you unless you're doing some awesome new feature !
 
    
    - 23,129
- 12
- 109
- 154
- 
                    
- 
                    2It was in a WWDC 2012 video. I'm going to rack my brains for which one it was now. TBC. – Daniel Nov 26 '12 at 19:11
- 
                    Thanks. I'm interested in watching that video if you remember which one it was. – Stephen Melvin Nov 26 '12 at 19:13
- 
                    4Okay got it: Session 212 Basics + Habits: Building Your Software To Last - at 13 minutes. https://developer.apple.com/videos/wwdc/2012/?include=212#212 – Daniel Nov 26 '12 at 19:36