Skip to content

Instantly share code, notes, and snippets.

@vl4dimir
Created January 4, 2011 15:29
Show Gist options
  • Save vl4dimir/764911 to your computer and use it in GitHub Desktop.
Save vl4dimir/764911 to your computer and use it in GitHub Desktop.
Stopwatch class used for cumulative runtime timing.
#import <Foundation/Foundation.h>
@interface Stopwatch : NSObject {
// Name to be used for logging
NSString* name;
// Total run time
NSTimeInterval runTime;
// The start date of the currently active run
NSDate* startDate;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, readonly) NSTimeInterval runTime;
- (id) initWithName:(NSString*)name;
- (void) start;
- (void) stop;
- (void) statistics;
@end
#import "Stopwatch.h"
@interface Stopwatch ()
@property (nonatomic, retain) NSDate* startDate;
@end
@implementation Stopwatch
@synthesize name;
@synthesize runTime;
@synthesize startDate;
- (id) initWithName:(NSString*)_name
{
if ((self = [super init])) {
self.name = _name;
runTime = 0;
}
return self;
}
- (void) start
{
self.startDate = [NSDate date];
}
- (void) stop
{
runTime += -[startDate timeIntervalSinceNow];
}
- (void) statistics
{
NSLog(@"%@ finished in %f seconds.", name, runTime);
}
- (void) dealloc
{
self.name = nil;
self.startDate = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment