Created
December 11, 2012 17:52
-
-
Save mikeash/4260612 to your computer and use it in GitHub Desktop.
Array iteration speed testing
This file contains 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
// clang -framework Foundation -fobjc-arc -O3 test.m | |
#import <Cocoa/Cocoa.h> | |
static void Time(dispatch_block_t block) | |
{ | |
NSProcessInfo *pi = [NSProcessInfo processInfo]; | |
NSTimeInterval start = [pi systemUptime]; | |
int iterations = 100; | |
for(int i = 0; i < iterations; i++) | |
block(); | |
NSTimeInterval end = [pi systemUptime]; | |
NSLog(@"Took %f seconds", (end - start) / iterations); | |
} | |
int main(int argc, char **argv) | |
{ | |
for(int i = 1; i <= 1000000; i *= 10) | |
{ | |
NSMutableArray *array = [NSMutableArray array]; | |
for(int j = 0; j < i; j++) | |
[array addObject: [[NSObject alloc] init]]; | |
NSLog(@"Testing with %d objects", i); | |
Time(^{ | |
for(id obj in array) | |
[obj self]; | |
}); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.matlab
?