在进行基准测试时,是什么原因导致CPU时间与“经过的实时时间"之间的滞后? [英] When benchmarking, what causes a lag between CPU time and "elapsed real time"?

查看:91
本文介绍了在进行基准测试时,是什么原因导致CPU时间与“经过的实时时间"之间的滞后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的基准测试模块来进行一些快速而肮脏的测试.它给了我:

I'm using a built-in benchmarking module for some quick and dirty tests. It gives me:

  • CPU时间
  • 系统CPU时间(实际上,我运行的代码对此一无所获)
  • 用户和系统CPU时间的总和(在我的情况下始终与CPU时间相同)
  • 经过的实时时间

我什至都不知道我需要所有这些信息.

I didn't even know I needed all that information.

我只想比较两段代码,看看哪一段需要更长的时间.我知道一段代码可能比另一段代码执行更多的垃圾回收,但是我不确定它会产生多大的影响.

I just want to compare two pieces of code and see which one takes longer. I know that one piece of code probably does more garbage collection than the other but I'm not sure how much of an impact it's going to have.

我应该考虑哪个指标?

Any ideas which metric I should be looking at?

而且,最重要的是,有人可以解释为什么经过的实时时间"总是比CPU时间长吗-造成两者之间滞后的原因是什么?

推荐答案

除了运行Ruby代码之外,系统中还有很多其他事情.经过的时间是实际花费的总时间,不应用于基准测试.您需要系统和用户的CPU时间,因为那是您的进程实际拥有CPU的时间.

There are many things going on in your system other than running your Ruby code. Elapsed time is the total real time taken and should not be used for benchmarking. You want the system and user CPU times since those are the times that your process actually had the CPU.

一个示例,如果您的过程是:

An example, if your process:

  • 使用CPU一秒钟来运行您的代码;然后
  • 使用CPU一秒钟运行OS内核代码;然后
  • 被交换了七秒钟,而另一个进程仍在运行;然后
  • 使用CPU多一秒钟来运行代码,

您将会看到:

  • 经过十秒钟的时间,
  • 两秒钟的用户时间,
  • 系统时间一秒,
  • 3秒的总CPU时间.

这三秒钟是您需要担心的,因为十秒钟完全取决于流程调度的变化情况.

The three seconds is what you need to worry about, since the ten depends entirely upon the vagaries of the process scheduling.

这篇关于在进行基准测试时,是什么原因导致CPU时间与“经过的实时时间"之间的滞后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆