One Quickie


Block that measures time. (Tools->General)
Sometimes you want to do a quick measurement of a chunk of code to see how long it takes, and you don't want to crank up gprof Shark Instruments to do it. mach_absolute_time is the finest-grained timer in the system. Here's a little utility to time a block:

#import <mach/mach_time.h>  // for mach_absolute_time() and friends

CGFloat BWTimeBlock (void (^block)(void)) {
    mach_timebase_info_data_t info;
    if (mach_timebase_info (&info) != KERN_SUCCESS) return -1;

    uint64_t start = mach_absolute_time ();
    block ();
    uint64_t end = mach_absolute_time ();
    uint64_t elapsed = end - start;

    uint64_t nanos = elapsed * info.numer / info.denom;
    return (CGFloat)nanos / NSEC_PER_SEC;

} // BWTimeBlock

And you would use it like:
    NSString *thing1 = @"hi";
    NSString *thing2 = @"hello there";

    time = BWTimeBlock(^{
            for (int i = 0; i < LOOPAGE; i++) {
                [thing1 isEqualTo: thing2];
            }
        });
    printf ("equalTo time: %f\n", time);



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com