追加 CString 比追加 ObjC String 更好吗?

发布于 2024-12-09 18:49:14 字数 355 浏览 1 评论 0原文

我正在编写一些进行字符串操作的代码。在这种特殊情况下,将“?partnerId=30”附加到 iTunes 附属链接的 URL 中。这是一个原始字符串并且完全静态。我在想,最好这样做:

urlString = [urlString stringByAppendingFormat:@"%@", @"?partnerId=30"];

或者:

urlString = [urlString stringByAppendingFormat:@"%s", "?partnerId=30"];

我认为最好不要实例化整个 Objective-C 对象,但我从未见过这样做。

I'm writing a bit of code doing string manipulation. In this particular situation, appending "?partnerId=30" to a URL for iTunes Affiliate linking. This is a raw string and completely static. I was thinking, is it better to do:

urlString = [urlString stringByAppendingFormat:@"%@", @"?partnerId=30"];

Or:

urlString = [urlString stringByAppendingFormat:@"%s", "?partnerId=30"];

I would think it's better to not instantiate an entire Objective-C object, but I've never seen it done that way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘑菇王子 2024-12-16 18:49:14

使用 @"" 语法声明的字符串是常量,并且在代码运行时已经存在于内存中,因此使用它们不会带来分配损失。

您可能会发现它们的速度稍微快一些,因为它们知道自己的长度,而 C 字符串需要循环才能找出它们的长度。

通过不使用格式字符串,您将获得更明显的性能改进(尽管仍然很小):

urlString = [urlString stringByAppendingString:@"?partnerId=30"];

String declared using the @"" syntax are constant and will already exist in memory by the time your code is running, thus there is no allocation penalty from using them.

You may find they are very slightly faster, as they know their own length, whereas C strings need to be looped through to find out their length.

Through you'd gain a more tangible performance improvement (though still tiny) from not using a format string:

urlString = [urlString stringByAppendingString:@"?partnerId=30"];
秋意浓 2024-12-16 18:49:14

文字 C 字符串和文字 NSString 都表示为内存的常量位。两者都不需要使用分配。

Both literal C strings and literal NSStrings are expressed as constant bits of memory. Neither requires an allocation on use.

帥小哥 2024-12-16 18:49:14

Objective-C 字符串文字是不朽的对象。当您的二进制文件加载到内存中时,它们就会被实例化。有了这些知识,前一种形式不会创建临时的NSString

老实说,我不知道一般来说哪个更快,因为这也取决于外部条件; NSString 可能表示多种编码的字符串(默认为 UTF-16),如果 urlString 需要执行编码转换,那么它可能会对 任一方法。无论哪种方式,它们都会非常快 - 我不会担心这种情况,除非您有许多(例如数千个)要创建的并且时间紧迫,因为它们的性能应该相似。

由于您使用的是以下形式:NSString = NSString+NSString,对于重要情况,NSString 文字可能会更快,因为长度与对象一起存储,并且两个字符串的编码可能已经与目标字符串匹配。您的示例中使用的 C 字符串转换为另一种编码也很简单,而且它很短。

C 字符串作为一种更原始​​的类型,如果您需要定义大量字符串,则可以减少加载时间和/或内存使用量。

对于简单的情况,我只会坚持使用 NSString 文字,除非问题比帖子暗示的要大得多。

如果您还需要一组给定文字的 C 字符串表示形式,那么您可能更愿意定义 C 字符串文字。定义 C 字符串文字还可能会强制您基于 C 字符串创建临时 NSString。在这种情况下,您可能需要为每种风格定义一个,或者使用 CFString 的“使用外部缓冲区创建 CFString”API。同样,这适用于非常不寻常的情况(如果您确实没有经历大量这些字符串,则可以进行微优化)。

Objective-C string literals are immortal objects. They are instantiated when your binary is loaded into memory. With that knowledge, the former form does not create a temporary NSString.

I honestly don't know which is faster in general, because it also depends on external conditions; An NSString may represent strings of multiple encodings (the default is UTF-16), if urlString has an encoding conversion to perform, then it could be a performance hit for either approach. Either way, they will both be quite fast - I wouldn't worry about this case unless you have many (e.g. thousands) of these to create and it is time critical, because their performance should be similar.

Since you are using the form: NSString = NSString+NSString, the NSString literal could be faster for nontrivial cases because the length is stored with the object and the encodings of both strings may already match the destination string. The C string used in your example would also be trivial to convert to another encoding, plus it is short.

C strings, as a more primitive type, could reduce your load times and/or memory usage if you need to define a lot of them.

For simple cases, I'd just stick with NSString literals in this case, unless the problem is much larger than the post would imply.

If you need a C string representation as well for a given set of literals, then you may prefer to define C string literals. Defining C string literals may also force you to create temporary NSStrings based on the C strings. In this case, you may want to define one for each flavor or use CFString's 'create CFString with external buffer' APIs. Again, this would be for very unusual cases (a micro-optimization if you are really not going through huge sets of these strings).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文