measureDuration<T> function

Future<T> measureDuration<T>(
  1. String label,
  2. Future<T> task(), {
  3. dynamic output(
    1. String text
    )?,
})

Implementation

Future<T> measureDuration<T>(
  String label,
  Future<T> Function() task, {
  Function(String text)? output,
}) async {
  if (!kDebugMode) {
    return task();
  }

  final stopwatch = Stopwatch()..start();

  try {
    final result = await task();

    stopwatch.stop();

    final text = '$label - completed in ${stopwatch.elapsedMilliseconds}ms';
    output?.call(text) ?? printDebug(text);

    return result;
  } catch (e) {
    stopwatch.stop();

    rethrow;
  }
}