如何测量一个程序的用户/系统CPU时间? [英] How to measure user/system cpu time for a piece of program?

查看:33
本文介绍了如何测量一个程序的用户/系统CPU时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在经典 APUE(UNIX 环境中的高级编程)的第 3.9 节中,作者测量了在其示例程序中消耗的用户/系统时间,该程序针对不同的缓冲区大小(I/O 读/写程序)运行.

In section 3.9 of the classic APUE(Advanced Programming in the UNIX Environment), the author measured the user/system time consumed in his sample program which runs against varying buffer size(an I/O read/write program).

结果表有点像(所有时间都以秒为单位):

The result table goes kinda like(all the time are in the unit of second):

BUFF_SIZE  USER_CPU   SYSTEM_CPU  CLOCK_TIME  LOOPS
1          124.89     161.65      288.64      103316352
...        
512        0.27       0.41        7.03        201789
...

我很好奇并且真的想知道如何测量一个程序的 USER/SYSTEM CPU 时间?

I'm curious about and really wondering how to measure the USER/SYSTEM CPU time for a piece of program?

在这个例子中,CLOCK TIME 是什么意思以及如何测量它?

And in this example, what does the CLOCK TIME mean and how to measure it?

显然它不仅仅是用户 CPU 时间和系统 CPU 时间的总和.

Obviously it isn't simply the sum of user CPU time and system CPU time.

推荐答案

您可以使用 *nix 下的 time 命令轻松测量程序的运行时间:

You could easily measure the running time of a program using the time command under *nix:

$ time myprog

real        0m2.792s
user        0m0.099s
sys         0m0.200s

realCLOCK_TIME 指的是挂钟时间,即从程序开始到结束所花费的时间,甚至包括其他进程在执行时所花费的时间片内核上下文切换它们.它还包括任何时候,进程被阻塞(I/O 事件等)

The real or CLOCK_TIME refers to the wall clock time i.e the time taken from the start of the program to finish and includes even the time slices taken by other processes when the kernel context switches them. It also includes any time, the process is blocked (on I/O events, etc.)

userUSER_CPU 是指在用户空间(即内核之外)花费的 CPU 时间.与 real 时间不同,它仅指特定进程占用的 CPU 周期.

The user or USER_CPU refers to the CPU time spent in the user space, i.e. outside the kernel. Unlike the real time, it refers to only the CPU cycles taken by the particular process.

sysSYSTEM_CPU 是指在内核空间中花费的 CPU 时间(作为系统调用的一部分).同样,这只是计算代表进程在内核空间中花费的 CPU 周期,而不是它被阻塞的任何时间.

The sys or SYSTEM_CPU refers to the CPU time spent in the kernel space, (as part of system calls). Again this is only counting the CPU cycles spent in kernel space on behalf of the process and not any time it is blocked.

time 实用程序中,usersys 是从 times()wait() 系统调用.real 通常使用 收集的 2 个时间戳中的时间差计算gettimeofday() 程序开始和结束时的系统调用.

In the time utility, the user and sys are calculated from either times() or wait() system calls. The real is usually calculated using the time differences in the 2 timestamps gathered using the gettimeofday() system call at the start and end of the program.

您可能还想知道的另一件事是real != user + sys.在多核系统上,usersys 或它们的总和很容易超过 real 时间.

One more thing you might want to know is real != user + sys. On a multicore system the user or sys or their sum can quite easily exceed the real time.

这篇关于如何测量一个程序的用户/系统CPU时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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