我已经制作了一些涉及解析html的片段,想知道代码是否运行缓慢,这是否可行?
答案 0 :(得分:19)
您可以使用Stopwatch来衡量执行时间:
Stopwatch stopwatch = new Stopwatch()..start();
doSomething();
print('doSomething() executed in ${stopwatch.elapsed}');
Dart 2:
new
final stopwatch = Stopwatch()..start();
doSomething();
print('doSomething() executed in ${stopwatch.elapsed}');
答案 1 :(得分:4)
如果您在网络上,您可以获得高分辨率计时器:
num time = window.performance.now();
来自http://api.dartlang.org/docs/releases/latest/dart_html/Performance.html#now
答案 2 :(得分:2)
在配置文件模式下使用devtools可获得最佳效果
import 'dart:developer';
Timeline.startSync('interesting function');
// iWonderHowLongThisTakes();
Timeline.finishSync();
https://flutter.dev/docs/testing/code-debugging#tracing-dart-code-performance