2013-02-24 01:45:47.483 Time[90294:303] NSMutableString : 1.177084 seconds
2013-02-24 01:45:49.632 Time[90294:303] NSMutableArray : 2.146141 seconds
Last active
December 14, 2015 03:19
-
-
Save Hardtack/5020393 to your computer and use it in GitHub Desktop.
NSMutableString vs NSMutableArray
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
// | |
// main.m | |
// Time | |
// | |
// Created by 최건우 on 13. 2. 24.. | |
// Copyright (c) 2013년 최건우. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSUInteger len = 10000000; | |
NSMutableArray *items = [NSMutableArray arrayWithCapacity:len]; | |
for (NSUInteger i = 0; i < len; i++) { | |
[items addObject:[NSString stringWithFormat:@"%ld", (unsigned long)i]]; | |
} | |
NSTimeInterval s; | |
// NSMutableString | |
NSMutableString *str = [NSMutableString string]; | |
s = [[NSDate date] timeIntervalSince1970]; | |
for (NSString *item in items) { | |
[str appendString:item]; | |
} | |
NSLog(@"NSMutableString : %f seconds", [[NSDate date] timeIntervalSince1970] - s); | |
// NSMutableArray | |
NSMutableArray* array = [NSMutableArray array]; | |
s = [[NSDate date] timeIntervalSince1970]; | |
for (NSString *item in items) { | |
[array addObject:item]; | |
} | |
NSString* result = [array componentsJoinedByString:@""]; | |
NSLog(@"NSMutableArray : %f seconds", [[NSDate date] timeIntervalSince1970] - s); | |
assert([result isEqualToString:str]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment