Dart API Referencedart:coreimplStopwatchImplementation

StopwatchImplementation class

A simple implementation of the Stopwatch interface.

class StopwatchImplementation implements Stopwatch {
  // The _start and _stop fields capture the time when [start] and [stop]
  // are called respectively.
  // If _start is null, then the [Stopwatch] has not been started yet.
  // If _stop is null, then the [Stopwatch] has not been stopped yet,
  // or is running.
  int _start;
  int _stop;

  StopwatchImplementation() : _start = null, _stop = null {}

  void start() {
    if (_start === null) {
      // This stopwatch has never been started.
      _start = _now();
    } else {
      if (_stop === null) {
        return;
      }
      // Restarting this stopwatch. Prepend the elapsed time to the current
      // start time.
      _start = _now() - (_stop - _start);
      _stop = null;
    }
  }

  void stop() {
    if (_start === null || _stop !== null) {
      return;
    }
    _stop = _now();
  }

  void reset() {
    if (_start === null) return;
    // If [_start] is not null, then the stopwatch had already been started. It
    // may running right now.
    _start = _now();
    if (_stop !== null) {
      // The watch is not running. So simply set the [_stop] to [_start] thus
      // having an elapsed time of 0.
      _stop = _start;
    }
  }

  int elapsed() {
    if (_start === null) {
      return 0;
    }
    return (_stop === null) ? (_now() - _start) : (_stop - _start);
  }

  int elapsedInUs() {
    return (elapsed() * 1000000) ~/ frequency();
  }

  int elapsedInMs() {
    return (elapsed() * 1000) ~/ frequency();
  }

  int frequency() => _frequency();

  external static int _frequency();
  external static int _now();
}

Implements

Stopwatch

Constructors

new StopwatchImplementation() #

StopwatchImplementation() : _start = null, _stop = null {}

Methods

int elapsed() #

Returns the elapsed number of clock ticks since calling start while the Stopwatch is running. Returns the elapsed number of clock ticks between calling start and calling stop. Returns 0 if the Stopwatch has never been started. The elapsed number of clock ticks increases by frequency every second.

docs inherited from Stopwatch
int elapsed() {
  if (_start === null) {
    return 0;
  }
  return (_stop === null) ? (_now() - _start) : (_stop - _start);
}

int elapsedInMs() #

Returns the elapsed counter converted to milliseconds.

docs inherited from Stopwatch
int elapsedInMs() {
  return (elapsed() * 1000) ~/ frequency();
}

int elapsedInUs() #

Returns the elapsed counter converted to microseconds.

docs inherited from Stopwatch
int elapsedInUs() {
  return (elapsed() * 1000000) ~/ frequency();
}

int frequency() #

Returns the frequency of the elapsed counter in Hz.

docs inherited from Stopwatch
int frequency() => _frequency();

void reset() #

Resets the elapsed count to zero. This method does not stop or start the Stopwatch.

docs inherited from Stopwatch
void reset() {
  if (_start === null) return;
  // If [_start] is not null, then the stopwatch had already been started. It
  // may running right now.
  _start = _now();
  if (_stop !== null) {
    // The watch is not running. So simply set the [_stop] to [_start] thus
    // having an elapsed time of 0.
    _stop = _start;
  }
}

void start() #

Starts the Stopwatch. The elapsed count is increasing monotonically. If the Stopwatch has been stopped, then calling start again restarts it without resetting the elapsed count. If the Stopwatch is currently running, then calling start does nothing.

docs inherited from Stopwatch
void start() {
  if (_start === null) {
    // This stopwatch has never been started.
    _start = _now();
  } else {
    if (_stop === null) {
      return;
    }
    // Restarting this stopwatch. Prepend the elapsed time to the current
    // start time.
    _start = _now() - (_stop - _start);
    _stop = null;
  }
}

void stop() #

Stops the Stopwatch. The elapsed count stops increasing. If the Stopwatch is currently not running, then calling stop does nothing.

docs inherited from Stopwatch
void stop() {
  if (_start === null || _stop !== null) {
    return;
  }
  _stop = _now();
}