【问题标题】:Dart is there a way to measure execution time for a small codeDart 有没有办法测量小代码的执行时间
【发布时间】:2013-06-06 06:30:30
【问题描述】:

我做了一些涉及解析html的sn-p,想知道代码是否运行缓慢,这可行吗?

【问题讨论】:

    标签: dart


    【解决方案1】:

    您可以使用Stopwatch 来测量执行时间:

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

    飞镖 2:

    • 类型推断
    • 可选new
    final stopwatch = Stopwatch()..start();
    doSomething();
    print('doSomething() executed in ${stopwatch.elapsed}');
    

    【讨论】:

    • 我可以在使用 stopwatch.elapsed 之前使用stopwatch.stop() 吗?
    • 是的,您可以在需要时停止秒表。
    • 仅供参考:elapsed 返回一个Duration,您可以调用inMillisecondsinSeconds 等来获得首选单位。
    【解决方案2】:

    如果你在网上,你可以得到一个高分辨率的计时器:

    num time = window.performance.now();
    

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

    【讨论】:

      【解决方案3】:

      在配置文件模式下使用 devtools 以获得最佳效果

      import 'dart:developer';
      
      Timeline.startSync('interesting function');
      // iWonderHowLongThisTakes();
      Timeline.finishSync();
      

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

      【讨论】:

      猜你喜欢
      • 2012-07-06
      • 2014-06-20
      • 1970-01-01
      • 2013-04-28
      • 2018-07-30
      • 2019-12-03
      • 2021-05-07
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多