c语言中设置计时器怎么编写源代码?例如:每格5秒输出字符a
工具/原料
方法/步骤
11. 如果是想使用秒级别的技术,可用使用C语言库自带的clock()进行计时。如: #include #include using namespace std; int main() { clock_t start = clock(); //do some process here clock_t end = (clock() - start)/CLOCKS_PER_SEC; cout<<'time comsumption is '<
22.如果想使用毫秒级别的计时可以使用2种方法。一种是使用linux的系统库,一种是使用CUDA的cutil的库。 A. 如果使用linux的系统库,则可以使用如下方法: #include int main() { timeval starttime,endtime; gettimeofday(&starttime,0); //do some process here gettimeofday(&endtime,0); double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec; timeuse /=1000;//除以1000则进行毫秒计时,如果除以1000000则进行秒级别计时,如果除以1则进行微妙级别计时 } timeval的结构如下: strut timeval { long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ };
3B. 如果可以使用CUDA的话,则可以使用CUDA的sdk里面的cutil库里面的函数。 #include int main() { unsigned int timer = 0; cutCreatTimer(&timer);//创建计时器 cutStartTimer(&timer);//开始计时 // do some process here cutStopTimer(&timer);//停止计时 cout<<'time is '<