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

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}');

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


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

Tags:

Dart