Created
November 9, 2012 01:14
-
-
Save eienf/4043089 to your computer and use it in GitHub Desktop.
time to produce a series of NSString
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <mach/mach_time.h> | |
// 0 .. 4 | |
#define TEST 0 | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSString *aString; | |
uint64_t start, elapsed; | |
mach_timebase_info_data_t base; | |
mach_timebase_info(&base); | |
start = mach_absolute_time(); | |
for ( int i = 0; i < 100; i++ ) { | |
@autoreleasepool { | |
aString = @""; | |
#if TEST == 0 | |
NSMutableString *muString = [NSMutableString string]; | |
for ( int j = 0; j < 100; j++ ) { | |
[muString appendFormat:@"Time Test (%d)\n",j]; | |
} | |
// time = 4343708(nsec) | |
#elif TEST == 1 | |
for ( int j = 0; j < 100; j++ ) { | |
aString = [aString stringByAppendingFormat:@"Time Test (%d)\n",j]; | |
} | |
// time = 25425541(nsec) | |
#elif TEST == 2 | |
for ( int j = 0; j < 100; j++ ) { | |
aString = [NSString stringWithFormat:@"%@\nTime Test (%d)",aString,j]; | |
} | |
// time = 19513834(nsec) | |
#elif TEST == 3 | |
NSMutableArray *anArray = [NSMutableArray array]; | |
for ( int j = 0; j < 100; j++ ) { | |
[anArray addObject:[NSString stringWithFormat:@"Time Test (%d)",j]]; | |
} | |
aString = [anArray componentsJoinedByString:@"\n"]; | |
// time = 13281454(nsec) | |
#elif TEST == 4 | |
NSMutableArray *anArray = [NSMutableArray arrayWithCapacity:100]; | |
for ( int j = 0; j < 100; j++ ) { | |
[anArray addObject:[NSString stringWithFormat:@"Time Test (%d)",j]]; | |
} | |
aString = [anArray componentsJoinedByString:@"\n"]; | |
// time = 12225972(nsec) | |
#endif | |
} | |
} | |
elapsed = mach_absolute_time() - start; | |
uint64_t nsec = elapsed * base.numer / base.denom; | |
printf("time = %lld(nsec)\n",nsec); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment