使用语言为C++,当然其他语言也可以就是有不同的包和方法。
先包含一下头文件
#include<iostream> #include<time.h>
#ifndef CLOCKS_PER_SEC //定义宏 #define CLOCKS_PER_SEC 1000 #endif // !CLOCKS_PER_SEC
接下来在main实现
int main() { clock_t start;//定义一个clock_t的对象为start long count = 0;//计数操作 start = clock();//clock()为获取当前的时间 while (1) { if ((clock() - start) == CLOCKS_PER_SEC)//间隔一秒 { count++; cout << count << " "; start = clock(); if (count == 10)//10秒跳出方法 { break; } } } }
本文章部分转载。