wjl
wjl

Reputation: 7361

Is it better to append a CString than an ObjC String?

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.

Upvotes: 3

Views: 168

Answers (3)

justin
justin

Reputation: 104698

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).

Upvotes: 1

grahamparks
grahamparks

Reputation: 16296

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"];

Upvotes: 4

Chuck
Chuck

Reputation: 237060

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

Upvotes: 3

Related Questions