简体   繁体   English

Mac 上的计时 C++ 程序

[英]Timing C++ program on Mac

I want to time my threading program on my mac and knew that for Linux environments, you can time it by doing: "time ./a.out".我想在我的 mac 上为我的线程程序计时,并且知道对于 Linux 环境,您可以通过执行以下操作来计时:“time ./a.out”。

I tried this on my mac terminal and it seems to be working but gives a little different output.我在我的 mac 终端上试过这个,它似乎可以工作,但输出有点不同。 The one on Linux will give output in the format: (random times btw) real 0m0.792s user 0m0.046s sys 0m0.218s Linux 上的输出格式为:(random times btw) real 0m0.792s user 0m0.046s sys 0m0.218s

while this one gives: ./a.out 0.84s user 1.49s system 29% cpu 7.866 total而这个给出:./a.out 0.84s user 1.49s system 29% cpu 7.866 total

What is the main difference between "real" and "user" and is there any other types of commands to time the execution of programs? “真实”和“用户”之间的主要区别是什么,是否还有其他类型的命令来计时程序的执行?

This is well explained in the man pages, in both Linux and macOS.这在 Linux 和 macOS 的手册页中有很好的解释。

Linux Linux

These statistics consist of (i) the elapsed real time between invocation and termination , (ii) the user CPU time and (iii) the system CPU time.这些统计数据包括 (i)调用和终止之间经过的实时时间,(ii) 用户 CPU 时间和 (iii) 系统 CPU 时间。

macOS苹果系统

time writes the total time elapsed, the time consumed by system overhead, and the time used to execute utility to the standard error stream. time 将经过的总时间、系统开销消耗的时间以及用于执行实用程序的时间写入标准错误流。

  • Real means the total time needed to execute the whole program, including startup and exit. Real表示执行整个程序所需的总时间,包括启动和退出。
  • User is basically your own code.用户基本上是您自己的代码。
  • System is the time spent calling system functions. System是调用系统函数所花费的时间。

The difference is not between "real" and "user";区别不在“真实”和“用户”之间; the numbers are also in a different order.数字的顺序也不同。

The difference is between "real" and "total", and there isn't one.区别在于“真实”和“总计”之间,并且没有一个。 They're just different ways of saying the same thing: total, wall-clock, real-world time to execute your program, including all user code, system calls and (crucially) any time that your CPU spent on other processes/tasks.它们只是说同一件事的不同方式:执行程序的总时间、挂钟时间、真实世界时间,包括所有用户代码、系统调用和(至关重要的)CPU 在其他进程/任务上花费的任何时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM