Created
January 4, 2011 15:29
-
-
Save vl4dimir/764911 to your computer and use it in GitHub Desktop.
Stopwatch class used for cumulative runtime timing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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