Skip to content

Instantly share code, notes, and snippets.

@Sija
Created February 7, 2011 22:01
Show Gist options
  • Save Sija/815329 to your computer and use it in GitHub Desktop.
Save Sija/815329 to your computer and use it in GitHub Desktop.
Objective-C benchmark utility class used in some of my projects
//
// Benchmark.h
// Opskrifter
//
// Created by Sijawusz Pur Rahnama on 03/02/09.
// Copyleft 2009. Some rights reserved.
//
#import <Foundation/Foundation.h>
@interface Benchmark : NSObject {
@protected
NSString * _name;
NSDate * _startTime;
NSDate * _endTime;
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * startTime;
@property (nonatomic, retain) NSDate * endTime;
// quick access class methods
+ (id) report:(NSString *)info with:(void (^)(void))block;
// singleton instances
+ (Benchmark *) instanceWithName:(NSString *)name;
// initializers
+ (id) benchmarkWithName:(NSString *)name;
- (id) initWithName:(NSString *)name;
// block method
- (id) run:(void (^)(void))block;
// benchmark methods
- (id) start;
- (id) stop;
- (id) log;
@end
//
// Benchmark.m
// Opskrifter
//
// Created by Sijawusz Pur Rahnama on 03/02/09.
// Copyleft 2009. Some rights reserved.
//
#import "Benchmark.h"
@implementation Benchmark
static NSMutableDictionary * __sharedBenchmarks = nil;
+ (NSMutableDictionary *) sharedBenchmarks {
if (!__sharedBenchmarks) {
__sharedBenchmarks = [[NSMutableDictionary alloc] init];
}
return __sharedBenchmarks;
}
+ (id) instanceWithName:(NSString *)name {
@synchronized (self) {
// get the benchmark or create it on-the-fly
id benchmark = [[self sharedBenchmarks] objectForKey:name];
if (!benchmark) {
benchmark = [self benchmarkWithName:name];
[[self sharedBenchmarks] setObject:benchmark forKey:name];
}
return benchmark;
}
return nil;
}
@synthesize name = _name;
@synthesize startTime = _startTime;
@synthesize endTime = _endTime;
# pragma mark -
# pragma mark Quick access class methods
+ (id) report:(NSString *)info with:(void (^)(void))block {
return [[self instanceWithName:info] run:block];
}
# pragma mark -
# pragma mark Initializers
+ (id) benchmarkWithName:(NSString *)name {
return [[[self alloc] initWithName:name] autorelease];
}
- (id) initWithName:(NSString *)name {
if (self = [self init]) {
self.name = name;
}
return self;
}
# pragma mark -
# pragma mark Benchmark methods
- (id) run:(void (^)(void))block {
[self start];
block();
[self stop];
return self;
}
- (id) start {
NSDate * start = [[NSDate alloc] init];
self.startTime = start;
[start release];
self.endTime = nil;
return self;
}
- (id) stop {
NSDate * stop = [[NSDate alloc] init];
self.endTime = stop;
[stop release];
[self log];
return self;
}
- (id) log {
NSTimeInterval timeElapsed;
if (_endTime) timeElapsed = [_endTime timeIntervalSinceDate:_startTime];
else timeElapsed = [_startTime timeIntervalSinceNow] * -1.f;
// log elapsed time
if (_name) NSLog(@"Benchmark '%@' took %f seconds.", _name, timeElapsed);
else NSLog(@"Benchmark took %f seconds.", timeElapsed);
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment