尝试 Ruby 的 Benchmark 模块...

>> Benchmark.bm(7) { |b| b.report('Report:') { s = '' ; 10000.times { s += 'a' } }  }
             user     system      total        real
Report:  0.150000   0.010000   0.160000 (  0.156361)

“用户”、“系统”和“真实”的含义是什么?

有帮助吗?

解决方案

这些时间与 Unix 相同 time 命令或其他典型的基准测试工具将报告:

  • 用户: :执行用户空间代码所花费的时间(即:你的代码),
  • 系统: :执行内核代码所花费的时间以及
  • 真实的: :执行代码所花费的“实际”时间(即 系统 + 用户 + 等待 I/O、网络、磁盘、用户输入等所花费的时间)。也称为“挂钟时间”。

其他提示

请检查此宝石: https://github.com/igorkasyanchuk/benchmark_methods

不再有这样的代码:

t = Time.now
user.calculate_report
puts Time.now - t

现在你可以做到:

benchmark :calculate_report # in class

然后调用你的方法

user.calculate_report
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top