You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ是否支持显示每行代码执行时间?(Dart语言)

解答

内置功能现状

IntelliJ(含WebStorm)没有直接在编辑器行尾显示单步代码执行耗时的内置功能,但调试模式下有两种间接获取时长的方式:

  • 断点计时:在目标行设置断点,右键断点选择「More」,勾选「Log evaluated expression」,输入Dart的DateTime.now().millisecondsSinceEpoch,每次断点触发时控制台会打印当前时间,自行计算差值即可得到该行代码的执行耗时。
  • 性能分析器(Profiler):以「Profile」模式运行程序,Dart Profiler会生成函数级别的执行时间报告,能查看User.init()这类方法的总耗时,但无法精确到单步代码的执行时长。

可用插件选项

目前针对Dart、支持编辑器内显示每行执行耗时的插件较少,可尝试以下方向:

  • Code Time:虽主打代码编写时长统计,但部分版本支持调试阶段的代码执行时长追踪,不过它的展示形式是侧边栏汇总,而非行尾注释。
  • 自定义脚本插件:比如「Script Runner」,可编写Dart脚本,配置后在调试时自动记录每行代码的执行时间并插入注释,灵活性高但需要自行编写脚本逻辑。

更实用的替代方案

如果需要精确追踪异步代码的执行耗时,手动在代码中添加计时逻辑更直接可靠,示例如下:

final stopwatch = Stopwatch()..start();
await User.init();
print('User.init(): ${stopwatch.elapsedMilliseconds}ms');
stopwatch.reset();

await Sett.initialize();
print('Sett.initialize(): ${stopwatch.elapsedMilliseconds}ms');
stopwatch.reset();

await Workspace.initialize();
print('Workspace.initialize(): ${stopwatch.elapsedMilliseconds}ms');
stopwatch.reset();

await SystemStore.initialize();
print('SystemStore.initialize(): ${stopwatch.elapsedMilliseconds}ms');

这种方式无需依赖IDE功能,结果精准,还能直接在控制台查看,适合调试阶段临时使用。

内容的提问来源于stack exchange,提问作者Bobin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:02:10