Skip to content

Instantly share code, notes, and snippets.

@groves
Created November 15, 2011 20:56
Show Gist options
  • Save groves/1368318 to your computer and use it in GitHub Desktop.
Save groves/1368318 to your computer and use it in GitHub Desktop.
SPPoint pooling vs arc
#import <mach/mach_time.h>
void logMachTime_withIdentifier_(uint64_t machTime, NSString *identifier) {
static double timeScaleSeconds = 0.0;
if (timeScaleSeconds == 0.0) {
mach_timebase_info_data_t timebaseInfo;
if (mach_timebase_info(&timebaseInfo) == KERN_SUCCESS) { // returns scale factor for ns
double timeScaleMicroSeconds = ((double) timebaseInfo.numer / (double) timebaseInfo.denom) / 1000;
timeScaleSeconds = timeScaleMicroSeconds / 1000000;
}
}
NSLog(@"%@: %g seconds", identifier, timeScaleSeconds*machTime);
}
// 0.46 seconds
- (void)testPooledPoints
{
uint64_t startTime, stopTime;
startTime = mach_absolute_time();
SPPoint *point = [[SPPoint alloc] initWithX:0.0 y:0.0];
for (int ii = 0; ii < 1000000; ii++) {
float newX = point.x + 1;
[point release];
point = [[SPPoint alloc] initWithX:newX y:0];
}
stopTime = mach_absolute_time();
STAssertEquals(1000000.0f, point.x, @"X should've been incremented to 1000000, not '%f'", point.x);
logMachTime_withIdentifier_(stopTime - startTime, @"10000000 pooled point instantiations");
}
// 0.52 seconds
- (void)testARCPoints
{
uint64_t startTime, stopTime;
startTime = mach_absolute_time();
SPPoint *point = [[SPPoint alloc] initWithX:0.0 y:0.0];
for (int ii = 0; ii < 1000000; ii++) {
float newX = point.x + 1;
point = [[SPPoint alloc] initWithX:newX y:0];
}
stopTime = mach_absolute_time();
STAssertEquals(1000000.0f, point.x, @"X should've been incremented to 1000000, not '%f'", point.x);
logMachTime_withIdentifier_(stopTime - startTime, @"10000000 ARC point instantiations");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment