3

In some unix shells there is the time command which prints how much time a given command takes to be executed. The output looks like

real  1m0.000s
user 10m0.000s
sys   0m0.000s

If I write a program that uses parallelization on multiple cores, the user-time can be a multiple of the real-time.

My question is, whether I can conclude, that if the user-time is very close to the real-time multiplied by the number of threads used, that the program is parallelized optimally? That is, that for example no thread has to wait for long periods of time for other threads.

  • 1
    Interesting. You would also need to take into account any other processes running at the same time which would also compete for resources. I don't know if you can make this measurement correctly from outside the parallelized process itself. – terdon Jul 19 '18 at 16:43
3

In one simple word: No.

What wastes a lot of effort is to switch between kernel space and user space, such switching is where the most waste is produced. There is (a lot) of work done just to get to where the real operation needs to be executed. The less switches needed the most efficient an operation should be.

There are operations that are completely done in kernel space (and there is no (safe) way to bypass that). In such cases, the most time is spent in kernel space, and that is the most efficient way to execute them.

There are other operations that must be executed in user space as the kernel has no service/function that implements it. In such operations, the more time that is used in user space, the more efficient the operation is.

But someone might have implemented an efficient kernel service in user space with some not-so-efficient algorithm. That will increase the user time but the result would be less efficient. Compared with the same service in kernel space.

And some other developer might be calling the kernel to read one byte at a time (and having to switch for every byte) instead of the equivalent call to read one meg at a time (if there is an equivalent function for a block instead of a byte).

And, in the end, it must be that some mix of kernel and user operations will be executed. To read a disk block, for example, the kernel should supply the function and it should be a "fire and forget" until the memory block (buffer) is filled with the result of the disk block read. To access process memory (like a program array), no kernel call should be needed.

There is no simple way to measure time efficiency.

  • 1
    I was about to write the same but turn the other way around, it's a good indicator if you have 4 cores and you're using 4 time the amount of CPU time compared to real time that your application is using almost all the CPU available at a moment. BUT, you have to know exactly what your application is doing to be sure about that? your application could waste computation power in inneficient loop or random access memory but that would be a job for a dev who knows how code runs and how compiler/interpreter work – Kiwy Jul 19 '18 at 17:40
  • 1
    @Kiwy Only if sys is zero. What conclusion could be drawn from real 0m10.261s user 0m0.016s sys 0m3.260s. That the process is very inefficient? That was a real time sudo dd if=/dev/sda of=/dev/null count=1000 bs=1M Almost no work done in user space but one giga of data read from disk. Or a result of real 0m11.060s user 0m3.596s sys 0m4.548s for executing the command time sudo dd if=/dev/sda count=1000 bs=1M | md5sum which is I/O limited (not cpu speed limited)? – Isaac Jul 19 '18 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.