具有线程的C ++中的时钟函数

问题描述:

这里有一个非常有趣的注释: http://en.cppreference. com/w/cpp/chrono/c/clock

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

仅由不同的对std :: clock的调用返回的两个值之间的差异才有意义,因为std :: clock时代的开始不必与程序的开始一致.std :: clock时间可能取决于操作系统提供给程序的执行资源,例如,如果CPU由其他进程共享,则std :: clock时间可能比墙上时钟慢. 另一方面,如果当前进程是多线程的,并且有多个执行核心可用,则std :: clock时间可能比挂钟快. "

"Only the difference between two values returned by different calls to std::clock is meaningful, as the beginning of the std::clock era does not have to coincide with the start of the program. std::clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system. For example, if the CPU is shared by other processes, std::clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, std::clock time may advance faster than wall clock."

为什么多线程加速时钟?我正在检查带有线程和不带有线程的C ++程序的性能,并且我注意到线程的时间类似(不太好),但是感觉更快(例如说3秒内8秒运行时).

Why does the clock speed up with multithreading? I'm checking the performance of a C++ program with threading vs without it and I'm noticing that the times are similar for threading (not better) but feel faster (like saying 8 seconds in 3 seconds of runtime).

如果有多个内核可用,并且您正在运行多个线程,则可能多个线程同时在不同的内核上执行.由于clock()衡量处理器时间,所以它的前进时间可能比挂钟时间快,因为有多个线程正在同时前进.

If more than one core is available, and you are running multiple threads, then potentially multiple threads are executing at the same time on different cores. Since clock() measures processor time, it may advance faster than wallclock time, because multiple threads are advancing it simultaneously.

就像文档中给出的示例一样-它显示了创建的两个线程,并且报告的clock()值几乎是报告的挂钟时间的两倍.

Just as the example given in the documentation - it shows two threads created, and the clock() value reported is almost double the wallclock time reported.