在没有 Benchmark 或 time 的情况下测量 Ruby 中的用户时间或系统时间
由于我目前正在进行一些时间测量,我想知道是否可以在不使用 Benchmark
类或命令行实用程序 time
。
使用 Time
类仅显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵活性的解决方案,例如
time = TimeUtility.now
# some code
user, system, real = TimeUtility.now - time
原因是我不喜欢 Benchmark
,因为它不能仅返回数字(编辑:我错了 - 它可以。请参阅下面的答案。)。当然,我可以解析输出,但这感觉不对。 *NIX 系统中的 time
实用程序也应该可以解决我的问题,但我想知道是否已经有某种在 Ruby 中实现的包装器,这样我就不需要自己进行这些系统调用。
多谢!
Since I'm doing some time measurements at the moment, I wondered if it is possible to measure the user time or system time without using the Benchmark
class or the command line utility time
.
Using the Time
class only reveals the wall clock time, not system and user time, however I'm looking for a solution which has the same flexibility, e.g.
time = TimeUtility.now
# some code
user, system, real = TimeUtility.now - time
The reason is that I somehow dislike Benchmark
, since it cannot return numbers only (EDIT: I was wrong - it can. See answers below.). Sure, I could parse the output, but that doesn't feels right. The time
utility from *NIX systems should solve my problem as well, but I wanted to know if there already is some kind of wrapper implemented in Ruby so I don't need to make these system calls by myself.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我重新阅读了 Benchmark 文档,发现它有一个名为
measure
的方法。这个方法正是我想要的:测量你的代码需要的时间并返回一个包含用户时间,系统时间,孩子的系统时间等的对象。它就像在这个过程中一样简单,我发现你可以添加自定义行基准输出。您可以使用它来获得两全其美的效果(自定义时间测量和最后的良好输出),如下所示:
结果将如下所示:
I re-read the Benchmark documentation and saw that it has a method named
measure
. This method does exactly what I want: Measure the time your code needs and returning an object which contains user time, system time, system time of childrens etc. It is as easy asIn the process I found out that you can add custom rows to the Benchmark output. You can use this to get the best of both worlds (custom time measurements and a nice output at the end) as follows:
The result will look like the following:
您可以使用
Process::times
函数,返回用户时间/系统时间。 (它不报告挂钟时间,您需要其他东西)。不过似乎有点版本或操作系统相关。这是它在我的系统(linux,ruby 1.8.7)上报告的内容:
尽管文档显示了这一点,所以某些版本/实现可能只有前两个:
请参阅
times
用于 Linux 上的底层调用。这是一个非常蹩脚的包装器,支持
-
:应该给你一些想法,让它在你的上下文中很好地工作。
You could use the
Process::times
function, which returns user time/system time. (It does not report wall clock time, you'll need something else for that). Seems to be a bit version or OS dependent though.This is what it reports on my system (linux, ruby 1.8.7):
The docs show this though, so some versions/implementations might only have the first two:
See
times
for the underlying call on Linux.Here's a really crappy wrapper that kind of supports
-
:Should give you ideas to make it work nicely in your context.
这个宝石可能会有所帮助:
https://github.com/igorkasyanchuk/benchmark_methods
不再有这样的代码:
现在你可以这样做:
并且只需调用你的方法
This gem might help:
https://github.com/igorkasyanchuk/benchmark_methods
No more code like this:
Now you can do:
And just call your method