51

I have a script converting video files and I run it at server on test data and measure its time by time. In result I saw:

real    2m48.326s
user    6m57.498s
sys     0m3.120s

Why real time is that much lower than user time? Does this have any connection with multithreading? Or what else?

Edit: And I think that script was running circa 2m48s

kobylecki
  • 613
  • 1
  • 5
  • 8
  • re your EDIT - that makes perfect sense, since `real` time is wall-clock time as explained below (ie what we would measure if we had a stop-watch) – Levon Jun 13 '12 at 17:39
  • 1
    Related: https://stackoverflow.com/questions/15928334/user-time-larger-than-real-time – try-catch-finally Aug 26 '17 at 06:53

4 Answers4

68

The output you show is a bit odd, since real time would usually be bigger than the other two.

  • Real time is wall clock time. (what we could measure with a stopwatch)
  • User time is the amount of time spend in user-mode within the process
  • Sys is the CPU time spend in the kernel within the process.

So I suppose if the work was done by several processors concurrently, the CPU time would be higher than the elapsed wall clock time.

Was this a concurrent/multi-threaded/parallel type of application?

Just as an example, this is what I get on my Linux system when I issue the time find . command. As expected the elapsed real time is much larger than the others on this single user/single core process.

real    0m5.231s
user    0m0.072s
sys     0m0.088s

The rule of thumb is:

  • real < user: The process is CPU bound and takes advantage of parallel execution on multiple cores/CPUs.
  • real ≈ user: The process is CPU bound and takes no advantage of parallel exeuction.
  • real > user: The process is I/O bound. Execution on multiple cores would be of little to no advantage.
Levon
  • 11,174
  • 4
  • 45
  • 41
  • I don't know if `avconv` is multi-threaded. May it be. `avconv` is the new generation of `ffmpeg`. I was converting 7 short flv files (about 20 seconds each). – kobylecki Jun 13 '12 at 15:59
  • _real time would usually be bigger than the other two_ - but I ask about other situation – kobylecki Jun 13 '12 at 16:17
  • 4
    This explanation is correct. It looks like this process was run on 4 cores. See also my explanation of [hyperthreading](http://unix.stackexchange.com/questions/18637/should-i-disable-hyperthreading-when-concerned-about-performance-of-single-threa/18645#18645) for more on how real/sys/user time is calculated. It doesn't relate exactly, but the concepts are the same. – bahamat Jun 13 '12 at 16:32
  • @kobylecki real time is less than the others because it looks like avconv is run on multiple cores. Since I don't know that software, nor how it was run, I don't want to make a 100% claim, but this is what it looks like based on the available information (3 lines of time measurements, and knowledge :-) – Levon Jun 13 '12 at 17:36
  • In the `find` example `usr` value is much lower because most of the time has been spent during interrupts, even if `find` would have been multithreaded it would have stayed low (sorry if I don't master english tenses). – Emmanuel Oct 07 '13 at 12:07
20

Just to illustrate what has been said, with a two threaded processes doing some calculation.

/*a.c/*
    #include <pthread.h>
    static void  * dosomething () {
        unsigned long a,b=1;
        for (a=1000000000; a>0; a--) b*=3;
        return NULL;
    }
    main () {
        pthread_t one, two;
        pthread_create(&one,NULL, dosomething, NULL);
        pthread_create(&two,NULL, dosomething, NULL);
        pthread_join (one, NULL);
        pthread_join (two, NULL);
    }
/* end of a.c */

compile

gcc a.c -lpthread

(This is just to illustrate, in real life I should have added the -D_REENTRANT flag)

$ time ./a.out

real    0m7.415s
user    0m13.105s
sys     0m0.032s

(Times are on an Intel Atom that has two slow cores :) )

Emmanuel
  • 4,167
  • 2
  • 23
  • 30
  • Hi I comment on this old entry, with no optimization the compiler don't detect that the function is not providing any result. When compiled with the `-O2` option the calculation are skipped. – Emmanuel May 23 '23 at 08:52
2

As Levon said:

I have a i7 (8 processors), so if run in bash:

$ time for ((i=0;i<1000000;i++)); do echo $i; done | wc -l
1000000

real    0m4,878s
user    0m4,368s
sys     0m3,083s

Then if restrict bash to one processos with taskset -cp 0 $BASHPID

$ taskset -cp 0 $BASHPID                                                                                         
pid 30551's current affinity list: 0-7
pid 30551's new affinity list: 0

now the result is

$ time for ((i=0;i<1000000;i++)); do echo $i; done | wc -l
1000000

real    0m7,120s
user    0m4,282s
sys     0m2,824s

https://www.looklinux.com/how-to-run-process-or-program-on-specific-cpu-cores-in-linux/

http://man7.org/linux/man-pages/man1/time.1.html

ton
  • 361
  • 3
  • 8
2

It doesn’t need to be multi-threaded program(s); you can get the same effect with multiple processes.  I wrote a C program to do some trivial number crunching:

$ time ./runner

real    1m12.746s
user    1m12.493s
sys     0m0.015s

Then I wrote a script to run two copies of the process:

$ cat run_script
#!/bin/sh
./runner &
./runner &
wait

$ time ./run_script

real    1m31.231s
user    3m0.538s
sys     0m0.215s