Get the App
SLTechnology News&Howtos  ›  Development  › 

How to gracefully calculate the running time of programs by linux

Shulou Source: shulou.com Published: 2022-06-01 05:12:39 10月04日 Update

Editor to share with you how elegant linux computing program running time, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Elegant computing program run time

Under Linux, you can easily get the run time of the program through the time command:

$time. / test

Real 0m1.003s

User 0m0.000s

Sys 0m0.000s

As you can see, the running time of the program is 1.003s. Careful students will see that real doesn't seem to be equal to user + sys, and it's much bigger than that. What's going on?

Let's first explain the meaning of these three parameters:

Real: clock time, that is, time spent from program execution to completion; user: time spent by cpu in user space during operation; sys: time spent by cpu in kernel space during operation

Because user and sys only count the time consumed by cpu, the call to sleep will block during the run of the program, or they may wait for the network or disk IO, which will consume a lot of time. So for similar cases, the value of real will be greater than the sum of the other two.

In addition, you will also encounter scenarios where real is much smaller than user + sys. What the heck is this?

This is easier to understand. If the program is parallel on multiple cpu, then the statistical time of user and sys is multiple cpu time, and the actual time consumed real is likely to be less than the sum of the other two.

These are all the contents of the article "how to elegantly calculate the running time of programs in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Time program running consumption elegance article sum content multiple situation space this is far statistical careful three not much two that is the kernel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Shulou Tech Info vpn Linux