This question already has answers here:
C++ calculating time intervals
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
我对C ++还是很陌生,我想知道如何实现一个简单的计时器,该计时器可以跟踪程序运行时经过了多少时间。例如,我怎么知道何时过了300秒?

最佳答案

#include <time.h>

clock_t t1,t2;

t1 = clock();


//Your code here

t2 = clock();

//Time taken for running your code segment
double time_dif = (double)(t2 - t1)/CLOCKS_PER_SEC;


实际上,t1-t2为您提供了执行期间的总时钟周期数,因此将其除以CLOCKS_PER_SEC即可得出实际时间

关于c++ - C++中的简单时间跟踪器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21592040/

10-11 22:07
查看更多