NSString hasPrefix: vs hasSuffix: which is cheaper?

In my project, I need to have a list of predefined prefix or suffix for string elements, can it be either a prefix or a suffix so that I can look at the elements and check the prefix or suffix of each element, just wondering, is it cheaper? hasPrefix: seems to be cheaper, but I have no links to support this.

thanks

+4
source share
1 answer

NSString is a "duty free bridge" to CFStringRef , so you can check the implementation at http://www.opensource.apple.com/source/CF/CF-744.19/CFString.c .

The corresponding functions are CFStringHasPrefix() and CFStringHasSuffix() , which call CFStringFindWithOptionsAndLocale() without or with the kCFCompareBackwards flag.

As I understand the code, this flag does not affect performance, only some loop variables are initialized differently.

(But the general “disclaimer” also applies here: you should profile your application and see if checking the prefix / suffix is ​​a performance bottleneck. If not, choose which is more logical or easier to maintain.)

+6
source

Source: https://habr.com/ru/post/1502739/


All Articles