Skip to content

Instantly share code, notes, and snippets.

@Hardtack
Last active December 14, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hardtack/5020393 to your computer and use it in GitHub Desktop.
Save Hardtack/5020393 to your computer and use it in GitHub Desktop.
NSMutableString vs NSMutableArray
//
// 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;
}

Result

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment