Skip to content

Instantly share code, notes, and snippets.

@chitza
Forked from vl4dimir/Stopwatch.h
Created July 5, 2012 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chitza/3055569 to your computer and use it in GitHub Desktop.
Save chitza/3055569 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