Dart - Measuring Elapsed Time with Stopwatch

This is a tutorial about how to use Stopwatch for measuring elapsed time in Dart.

If you are using Dart and you need to measure how long does a process take to complete, one of the easiest way is using Stopwatch class.

The class has elapsedTicks property, which keeps track the elapsed number of clock ticks since calling start(). Below are the lists of its properties and methods.


Name Type Description
elapsed Duration elapsedTicks as Duration object.
elapsedMicroseconds Duration elapsedTicks in microseconds.
elapsedMilliseconds Duration elapsedTicks in milliseconds.
elapsedTicks Duration elapsedTicks
frequency int Elapsed counter frequency (Hz)
isRunning bool Whether the stopwatch is running.
hashCode int The hash code for this object.
runtimeType Type A representation of the runtime type of the object.


Name Type Description
reset() void Reset time to 0.
start() void Start the stopwatch.
stop() void Stop the stopwatch.


  Stopwatch s = new Stopwatch();
  sleep(new Duration(seconds: 1));
  print(s.elapsedMilliseconds); // 0

  sleep(new Duration(seconds: 2));
  print(s.isRunning); // true
  print(s.elapsedMilliseconds); // around 2000ms

  sleep(new Duration(seconds: 1));
  print(s.elapsedMilliseconds); // around 3000ms
  print(s.isRunning); // false

  sleep(new Duration(seconds: 1));
  print(s.elapsedMilliseconds); // around 3000ms

  print(s.elapsedMilliseconds); // 0

To use Stopwatch, first we have to create an instance of it by calling its constructor which has no argument. However it won't start to measure the time before start() is called. You can see the output of the above code, the first elapsedMilliseconds is 0 even after delay.

By calling start(), the Stopwatch is started. After around 2 seconds delay, the elapsed time should be 2 seconds and isRunning should be true.

Then, the Stopwatch is stopped (by calling .stop()) after 1 second delay. The elapsed time should be around 3 seconds. Keep in mind that the elapsed time may not exactly 3 seconds (the total delay time) as it may take a few milliseconds to execute statements.

After another 1 second delay, the elapsed time still remains at 3 seconds as it has been stopped.

By calling reset(), the counter is resetted to 0.

That's how to use Stopwatch in Dart.