Clock function in C ++ with threads

It is really interesting to note here: http://en.cppreference.com/w/cpp/chrono/c/clock

"Only the difference between the two values โ€‹โ€‹returned by various calls to std :: clock makes sense, since the beginning of the era std :: clock does not have to coincide with the beginning of the program. STD :: clock time may be faster or slower than a wall clock, depending from execution resources provided by the operating system by the operating system. For example, if the CPU is used by other processes, the time std :: clock may advance more slowly than the wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, in Remy std :: clock can accelerate faster than a wall clock. "

Why is the clock accelerated with multithreading? I check the performance of a C ++ program using streaming without it, and I notice that times are like streams (no better), but they feel faster (for example, they say 8 seconds in 3 seconds of execution time).

+3
source share
1 answer

If more than one core is available and you are using multiple threads, then potentially multiple threads are simultaneously running on different cores. Since it clock()measures the processorโ€™s time, it can move faster than the wall-mounted time, as several threads advance it at the same time.

, , - , , clock(), , , .

+2

Source: https://habr.com/ru/post/1691376/


All Articles