ZetCode

Dart StopWatch

last modified June 4, 2025

The StopWatch class in Dart is designed for precise time measurement, making it ideal for performance analysis and benchmarking. It provides high-resolution timing to track elapsed time efficiently.

Part of the dart:core library, StopWatch requires no additional imports and offers time measurement in milliseconds and microseconds. On supported platforms, it can provide nanosecond precision, ensuring highly accurate timing for critical operations.

Basic StopWatch Usage

The simplest way to use StopWatch is to start it, perform operations, and stop it. The elapsed time can then be retrieved.

main.dart
void main() {
  var stopwatch = Stopwatch();
  
  stopwatch.start();
  // Simulate work
  for (var i = 0; i < 1000000; i++) {}
  stopwatch.stop();
  
  print('Elapsed time: ${stopwatch.elapsedMilliseconds} ms');
}

This example creates a StopWatch, starts it, performs a loop, stops it, and prints the elapsed time in milliseconds. The elapsedMilliseconds property returns the total elapsed time.

$ dart main.dart
Elapsed time: 2 ms

Measuring Multiple Intervals

StopWatch can measure multiple intervals by starting and stopping it multiple times. The elapsed time accumulates across all intervals.

main.dart
void main() {
  var stopwatch = Stopwatch()..start();
  
  // First operation
  for (var i = 0; i < 500000; i++) {}
  stopwatch.stop();
  
  print('First interval: ${stopwatch.elapsedMicroseconds} μs');
  
  // Second operation
  stopwatch.start();
  for (var i = 0; i < 1000000; i++) {}
  stopwatch.stop();
  
  print('Total time: ${stopwatch.elapsedMicroseconds} μs');
}

Here we measure two separate operations while accumulating the total time. The StopWatch continues counting from where it left off when restarted.

$ dart main.dart
First interval: 1000 μs
Total time: 3000 μs

Reset and Reuse

StopWatch can be reset to zero and reused for new measurements. This is useful for repeated benchmarking.

main.dart
void main() {
  var stopwatch = Stopwatch();
  
  // First measurement
  stopwatch.start();
  performOperation();
  stopwatch.stop();
  print('First run: ${stopwatch.elapsedMilliseconds} ms');
  
  // Reset and measure again
  stopwatch.reset();
  stopwatch.start();
  performOperation();
  stopwatch.stop();
  print('Second run: ${stopwatch.elapsedMilliseconds} ms');
}

void performOperation() {
  // Simulate work
  for (var i = 0; i < 1000000; i++) {}
}

The reset method sets the elapsed time back to zero. This allows the same StopWatch instance to be reused for multiple independent measurements.

$ dart main.dart
First run: 2 ms
Second run: 1 ms

Microsecond Precision

For more precise measurements, StopWatch provides microsecond-level timing through the elapsedMicroseconds property.

main.dart
void main() {
  var stopwatch = Stopwatch()..start();
  
  // Very short operation
  var sum = 0;
  for (var i = 0; i < 100; i++) {
    sum += i;
  }
  
  stopwatch.stop();
  
  print('Operation took:');
  print('${stopwatch.elapsedMicroseconds} microseconds');
  print('${stopwatch.elapsedMilliseconds} milliseconds');
}

This example shows the difference between microsecond and millisecond precision. For very short operations, microseconds provide more granular measurement.

$ dart main.dart
Operation took:
50 microseconds
0 milliseconds

Measuring Code Blocks

StopWatch can be used to compare the performance of different code implementations. This helps identify bottlenecks and optimize critical sections.

main.dart
void main() {
  var stopwatch = Stopwatch();
  const size = 10000;
  
  // Measure List creation
  stopwatch.start();
  var list1 = List.generate(size, (i) => i);
  stopwatch.stop();
  print('List.generate: ${stopwatch.elapsedMicroseconds} μs');
  
  stopwatch.reset();
  
  // Measure manual List creation
  stopwatch.start();
  var list2 = [];
  for (var i = 0; i < size; i++) {
    list2.add(i);
  }
  stopwatch.stop();
  print('Manual creation: ${stopwatch.elapsedMicroseconds} μs');
}

This example compares two ways to create a list in Dart. The StopWatch helps determine which approach is faster for a given size of collection.

$ dart main.dart
List.generate: 1200 μs
Manual creation: 800 μs

Best Practices

Source

Dart StopWatch Documentation

This tutorial covered Dart's StopWatch class with practical examples showing how to measure and compare code execution times effectively.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.