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

Dart

Dart Problem Overview


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

Dart Solutions


Solution 1 - Dart

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

Solution 2 - Dart

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

Solution 3 - Dart

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFaris NasutionView Question on Stackoverflow
Solution 1 - DartAlexandre ArdhuinView Answer on Stackoverflow
Solution 2 - DartSeth LaddView Answer on Stackoverflow
Solution 3 - DartJhionan SantosView Answer on Stackoverflow