Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created December 11, 2012 17:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeash/4260612 to your computer and use it in GitHub Desktop.
Save mikeash/4260612 to your computer and use it in GitHub Desktop.
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
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