为什么real time大于user+system+total?

Why is real time more than user+system+total?

我 运行 一个基准测试做 运行dom 事情看看这些事情需要多少时间:

                              user     system      total        real
joining an array of strings  0.040000   0.010000   0.050000 (  0.046636)
string interpolation         0.020000   0.000000   0.020000 (  0.023903)
just timing                  0.000000   0.000000   0.000000 (  0.004334)

考虑到我有一个多核处理器,我猜想实际时间可能少于总时间,但在最后一种情况下,只报告实时时间,而其他时间为 0。我不明白这是怎么回事案.

如果相关,这些是我的基准测试:

require 'benchmark'

iterations = 10_000

Benchmark.bm do |bm|
  bm.report('joining an array of strings') do
    iterations.times do
      ["The","Current","Time","Is",Time.now].join(" ")
    end
  end

  bm.report('string interpolation') do
    iterations.times do
      "The current time is #{Time.now}"
    end
  end

  bm.report('just timing') do
    iterations.times do
      Time.now
    end
  end

end

编辑:所以我将 iterations 提高到 iterations = 1_000_000,这些是新结果:

                               user     system      total        real
joining an array of strings  3.640000   0.000000   3.640000 (  3.644826)
string interpolation         2.390000   0.000000   2.390000 (  2.393069)
just timing                  0.390000   0.000000   0.390000 (  0.392369)

实际时间似乎仍然超过总时间,我仍然不明白,即使上次测试所用的时间低于分辨率,那么实时时间也应该为 0,这不是案例.

进程可以处于阻塞等待状态,主要是在等待 IO 时。在此期间,进程不在 CPU 上,因此不计算 CPU 时间。

总计和系统+用户之间差异较大的另一个原因是系统负载如此之高,进程经常打开和关闭CPU。