Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't use <code>NSTimer</code> 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.</p> <p>What you want is a <strong>High resolution timer</strong> class (using <code>NSDate</code>):</p> <p><strong>Output:</strong></p> <pre><code>Total time was: 0.002027 milliseconds Total time was: 0.000002 seconds Total time was: 0.000000 minutes </code></pre> <p><strong>Main:</strong></p> <pre><code>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]); </code></pre> <p><strong>Edit:</strong> Added methods for <code>-timeElapsedInMilliseconds</code> and <code>-timeElapsedInMinutes</code></p> <p><strong>Timer.h:</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Timer : NSObject { NSDate *start; NSDate *end; } - (void) startTimer; - (void) stopTimer; - (double) timeElapsedInSeconds; - (double) timeElapsedInMilliseconds; - (double) timeElapsedInMinutes; @end </code></pre> <p><strong>Timer.m</strong></p> <pre><code>#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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload