Cpu Time Vs Wall Time 2022: Which Is Better And Why?

Your process might be slow because it is very CPU-intensive. Or maybe it’s blocking I/O (network, filesystem) or locks. How can you tell?

Your process will only get small task slices between processing requests. Each task slice is added to the total execution time. The CPU time spent processing other requests is not included in your total execution time.

There are many answers. In this article, Medcpu will focus on comparing Cpu Time Vs Wall Time.

Wall clock time vs CPU time

The CPU time is not the same as wall time (as in, a clock on the wall). CPU time refers to the total execution time of a process.

Wall clock time vs CPU time

Your process will only get small task slices between processing requests. Each task slice is added to the total execution time. The CPU time spent processing other requests is not included in your total execution time.

Let’s take, for example, gzip on 20MB Apache log files to see how long it took.

  • $ time gzip access.log
  • Real 0m2.125s
  • User 0m1.920s
  • sys 0.1770s

The process took 2.125 seconds of wall time (“real”) and 1.920 seconds of CPU time (“user”), while 0.170 seconds was spent in kernel mode (“sys”). The time spent on other processes was 0.035 seconds. (Remember that compressing files is a very intensive task that consumes a lot of CPU time on the server.

Some processes take a lot of CPU time but require little wall time. The next example illustrates the processing time required to run ‘find’ on the HC01 server’s /big/dom trees.

  • $ time find /big/dom -ls
  • Real 0m37.707s
  • User 0m1.210s
  • sys 0m1.260s

This task has a large difference between CPU and wall time. It took 35.237 seconds to complete the task. The time was sliced for other processes.

Because it takes over 50,000 files for the screen to print, the Linux Kernel’s scheduling system was free to allow other processes to have more time slices. This is because of the extra processing overhead required to print text to a screen using the print function.

This example will eliminate the delay in displaying the file list on the screen.

  • $ time find /big/dom -ls > /dev/null
  • Real 0m1.164s
  • User 0m0.900s
  • sys 0.260s

There is now a difference in time between the CPU time and wall time of 0.004 seconds.

Read also:

13 Vs 15 Inch Laptop 2022: Which Is Better And Why?

24 Vs 27 Monitor 2022: Which Is Better And Why?

How To Use Laptop As Monitor For Ps4

What you can learn from different ratios

The CPU clock time was less than the wall clock time in the above example, but there are other possible relationships. It is easier to express the potential relationship as a ratio (CPU time / (wallclock time), CPU/second.

What you can learn from different ratios

If this is a single-threaded process:

  • CPU/second 1: The CPU was used by the entire process. The program will run faster if it has a faster CPU.
  • CPU/second 1. The smaller the number, the longer the process takes to wait (for the network or harddrive or locks or any other processes to release the CPU or just sleeping). For example, If CPU/second was 0.75, 25% time was spent waiting.

This can be a multi-threaded task if your computer has at least N CPUs and N threads. In this case, CPU/second could be as high N.

  • CPU/second = 1: The process spent a lot of time waiting.
  • CPU/second N: All CPUs were saturated.

Other values: This process involved some combination of CPU and waiting. It can be difficult to determine which bottleneck is with this measurement alone. The number of threads can be used to help you determine if you are saturated. If you have 2 threads running and the CPU/second is 2, then you are saturated.

CPU usage over time with the psutil Python library

We’ve only been looking at CPU usage over the entire process, but it is possible to measure it over time. The psutil library in python timer makes it easy to measure CPU usage over time. It provides a lot of useful information about processes.

Conclusion

There you have it – the comparison between wall time vs CPU time. There is no cut-and-dried rule for how the CPU time for a process will be sliced as it mostly depends on the processing requirements of the task and how many other processes are vying for the CPU’s attention. Please leave a comment below if this article helped you.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top