Skip to content

Instantly share code, notes, and snippets.

@mikeash mikeash/gist:4260612
Created Dec 11, 2012

Embed
What would you like to do?
Array iteration speed testing
// 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;
}
@a2

This comment has been minimized.

Copy link

a2 commented Dec 12, 2012

.matlab?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.