I am trying to make a stop watch with NSTimer.
I gave the following code:
nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO];
and it is not working in milliseconds. It takes more than 1 millisecond.
There are sections on "Creating and Scheduling a Timer", "Stopping a Timer" and "Memory Management". From the article, creating a scheduled, non-repeating timer can be done something like this: [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:NO];
A timer that fires after a certain time interval has elapsed, sending a specified message to a target object.
Don't use NSTimer
that way. NSTimer is normally used to fire a selector at some time interval. It isn't high precision and isn't suited to what you want to do.
What you want is a High resolution timer class (using NSDate
):
Output:
Total time was: 0.002027 milliseconds Total time was: 0.000002 seconds Total time was: 0.000000 minutes
Main:
Timer *timer = [[Timer alloc] init]; [timer startTimer]; // Do some work [timer stopTimer]; NSLog(@"Total time was: %lf milliseconds", [timer timeElapsedInMilliseconds]); NSLog(@"Total time was: %lf seconds", [timer timeElapsedInSeconds]); NSLog(@"Total time was: %lf minutes", [timer timeElapsedInMinutes]);
Edit: Added methods for -timeElapsedInMilliseconds
and -timeElapsedInMinutes
Timer.h:
#import <Foundation/Foundation.h> @interface Timer : NSObject { NSDate *start; NSDate *end; } - (void) startTimer; - (void) stopTimer; - (double) timeElapsedInSeconds; - (double) timeElapsedInMilliseconds; - (double) timeElapsedInMinutes; @end
Timer.m
#import "Timer.h" @implementation Timer - (id) init { self = [super init]; if (self != nil) { start = nil; end = nil; } return self; } - (void) startTimer { start = [NSDate date]; } - (void) stopTimer { end = [NSDate date]; } - (double) timeElapsedInSeconds { return [end timeIntervalSinceDate:start]; } - (double) timeElapsedInMilliseconds { return [self timeElapsedInSeconds] * 1000.0f; } - (double) timeElapsedInMinutes { return [self timeElapsedInSeconds] / 60.0f; } @end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With