php 中的 cpu_get_usage ?
我创建了一个基准类,允许用户插入例如
$timer->checkpoint('1');
检查一些代码的时间、内存消耗等......并且在代码的末尾,如果她/他想测试它,她/他必须插入
$result=$timer->result();
此函数会向公共函数 result() 提供一些数据,例如内存使用情况(使用 memory_get_peak_usage)和时间消耗(microtime())。
这一切对我来说都很好。
但是如何使用现有内置 php 函数的组合来获得一个可以视为 CPU 消耗的值?
使用内置函数计算某段代码花费了多少时间非常容易,但我一直在思考如何获取某段代码的 CPU 消耗情况。
I have created a benchmark class that allows the user to insert for example
$timer->checkpoint('1');
to check out some code for time,memory consumption and such.... and at the end of the code if she/he wants to test it she/he has to insert
$result=$timer->result();
this gives out some data to public function result() like e.g. memory usage (using memory_get_peak_usage) and time consumption (microtime()).
This all works just fine for me.
But how can I use the combination of existing built-in php functions to get a value that can be considered a CPU consumption?
It has been quite easy to calculate how much time has been spent on a certain piece of code using the built-in functions, but I've been having trouble thinking of a way how to get the CPU consumption for a certain piece of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不想回答我自己的问题,但我做了很多研究,现在我开始......
基本上我所做的就是这样...
正如你所看到的,我使用了 getrusage php 内置函数。它给出了一个包含大量信息的数组,其中大多数对我来说毫无用处,除了 ru_utime.tv_usec(用户时间)和 ru_stime.tv_usec(系统时间)。要查看脚本消耗了多少 CPU 功率,我们需要查看“用户时间”和“系统时间”值,正如您在代码中看到的那样,将其相加即可获取实际上是 CPU 使用情况的时间。
Hate to answer my own question but I did a lot of research, and here I go...
Basically what I did was this...
As you can see I used a getrusage php built-in function. Which gives out an array with a whole bunch of info, of which most was pretty useless to me, except ru_utime.tv_usec(user time) and ru_stime.tv_usec(system time). To see how much CPU power the script has consumed, we need to look at the 'user time' and 'system time' values and as you can see in the code, add it to get the time which is in fact a cpu usage.
用法:
在简单的情况下,它给出的结果与 microtime 相同,但精度较低。
microtime
具有相同的功能:当然,不要像这样使用:
Usage:
In simple cases it gives the same, as
microtime
, but with less precision.The same function for
microtime
:Of coarse, not to use like this:
怎么样?
And how about..?