Skip to content

Instantly share code, notes, and snippets.

@groves
Created November 15, 2011 20:56
Show Gist options
  • Select an option

  • Save groves/1368318 to your computer and use it in GitHub Desktop.

Select an option

Save groves/1368318 to your computer and use it in GitHub Desktop.

Revisions

  1. groves created this gist Nov 15, 2011.
    55 changes: 55 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #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");
    }