Dart is there a way to measure execution time for a small code

I've made some snippet involving parsing a html and wants to know if the code runs slow or not, is this doable?


You can use Stopwatch to measure execution time :

Stopwatch stopwatch = new Stopwatch()..start();
doSomething();
print('doSomething() executed in ${stopwatch.elapsed}');

Dart 2:

  • Type inference
  • Optional new
final stopwatch = Stopwatch()..start();
doSomething();
print('doSomething() executed in ${stopwatch.elapsed}');

If you are on the web, you get can a high resolution timer:

num time = window.performance.now();

From http://api.dartlang.org/docs/releases/latest/dart_html/Performance.html#now


Use devtools in profile mode for the best result

import 'dart:developer';

Timeline.startSync('interesting function');
// iWonderHowLongThisTakes();
Timeline.finishSync();

https://flutter.dev/docs/testing/code-debugging#tracing-dart-code-performance