Skip to content

Instantly share code, notes, and snippets.

@Catfish-Man
Last active February 6, 2022 14:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Catfish-Man/051b49472d2f791f0f68eeb35b6244e8 to your computer and use it in GitHub Desktop.
Save Catfish-Man/051b49472d2f791f0f68eeb35b6244e8 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
const long count = 10000000;
const char *str1 = "hellohello"; //fits in a tagged pointer
const char *str2 = "hellohelQo"; //at this character count, can only fit in a tagged pointer by using <8 bits per character, which requires dropping less frequently used chars like 'Q'
const char *str3 = "Northern Sami"; //this string is encountered frequently enough that it's baked into a perfect hash table in CoreFoundation
const char *str4 = "hellohellü"; //this string can't be tagged *and* can't be stored using an ASCII backing store
NSDate *start = [NSDate date];
for (long i = 0; i < count; i++) {
(void)[[NSString alloc] initWithUTF8String:str1];
}
NSTimeInterval t = -[start timeIntervalSinceNow];
NSLog(@"Time to allocate @\"%s\" %ld times was %f seconds", str1, count, t);
start = [NSDate date];
for (long i = 0; i < count; i++) {
(void)[[NSString alloc] initWithUTF8String:str2];
}
t = -[start timeIntervalSinceNow];
NSLog(@"Time to allocate @\"%s\" %ld times was %f seconds", str2, count, t);
start = [NSDate date];
for (long i = 0; i < count; i++) {
(void)[[NSString alloc] initWithUTF8String:str3];
}
t = -[start timeIntervalSinceNow];
NSLog(@"Time to allocate @\"%s\" %ld times was %f seconds", str3, count, t);
start = [NSDate date];
for (long i = 0; i < count; i++) {
(void)[[NSString alloc] initWithUTF8String:str4];
}
t = -[start timeIntervalSinceNow];
NSLog(@"Time to allocate @\"%@\" %ld times was %f seconds", [[NSString alloc] initWithUTF8String:str4], count, t);
}
return 0;
}
/* On a 2.5GHz 2014 Retina MacBook Pro running 10.12.x, compiled with clang -fobjc-arc -framework Foundation -Os oddstrings.m -o odd, this produces:
2016-11-30 11:17:12.120 odd[85251:12659427] Time to allocate @"hellohello" 10000000 times was 0.385130 seconds
2016-11-30 11:17:13.957 odd[85251:12659427] Time to allocate @"hellohelQo" 10000000 times was 1.834985 seconds
2016-11-30 11:17:14.582 odd[85251:12659427] Time to allocate @"Northern Sami" 10000000 times was 0.625479 seconds
2016-11-30 11:17:17.261 odd[85251:12659427] Time to allocate @"hellohellü" 10000000 times was 2.678630 seconds
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment