Stopwatch

I won't spend too much time here. InstantStopwatch is Instant's version of the dart:async Stopwatch class.

While a nice utility class, it's almost a mirror of the original, dart:async Stopwatch class in terms of methods. So why use it?

There's a few reasons, and they all have to do with practicality:

  • Works through sleep(): This is the main reason for this class. The old Stopwatch class can't work through a sleep() method. This is a definite advantage for InstantStopwatch, becuase it can provide accurate runtimes, whereas the old Stopwatch can't.

  • Nothing running in the background: There is literally no process running in the background after you instantiate and start an InstantStopwatch. It just uses a complex system of DateTime differences to show you the exact time it's been unpaused.

  • MAJOR performance boost: In various benchmarks, this has proven to be 2-3x faster than the original Stopwatch class.

This is all to say, if none of the above helps you, don't bother with this class. But if something above intrigues you, here's a brief usage demo. It probably looks somewhat familiar:

InstantStopwatch watch = InstantStopwatch(); //watch
watch.play(); //play
print(watch.getStopwatchInSeconds()); //0
sleep(Duration(seconds: 2));
print(watch.getStopwatchInSeconds()); //2
watch.pause(); //pause
sleep(Duration(seconds: 2));
print(watch.getStopwatchInSeconds()); //2
watch.play(); //play
sleep(Duration(seconds: 2));
print(watch.getStopwatchInSeconds()); //4

Last updated