简体   繁体   English

NSString stringWithFormat很慢

[英]NSString stringWithFormat is slow

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). 在Objective-C中,方法stringWithFormat:似乎非常慢,实际上是我们的一个应用程序中的一个大瓶颈(我们使用分析器来查找它)。 Is there a way to optimise it or use some faster C code? 有没有办法优化它或使用更快的C代码?

Yes use sprintf in c http://www.cplusplus.com/reference/cstdio/sprintf/ after that push the char* in a NSString with [NSString stringWithUTF8:]; 是的,使用sprintf在c http://www.cplusplus.com/reference/cstdio/sprintf/之后使用[NSString stringWithUTF8:];在NSString中推送char * [NSString stringWithUTF8:];

example: 例:

char cString[255];
sprintf (cString, "%d", 36);
NSString* OCstring = [[NSString alloc] initWithUTF8String:cString];

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. 如果您正在进行大量的字符串操作和操作 - 听起来您可能正在这样做,并且NSString确实成为您的应用程序的瓶颈,我建议尝试使用C ++来满足您的字符串需求而不是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. Apple承认虽然NSString很棒,但事实上,它是顶级的,因为他们的iOS自动更正算法遇到了类似的问题, NSString计算速度太慢,并且比较了很多东西。 They then switched to C++ and got all the performance they needed. 然后他们切换到C ++并获得他们所需的所有性能。

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 ! 你应该放一些代码,我很惊讶这发生在你身上,除非你做了一些很棒的新功能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM