使用ctime.h头文件来控制程序延时秒数

ctime.h 头文件定义了一个符号常量:CLOCKS_PER_SEC,该常量等于每秒包含的系统时间单位数,这个单位数在头文件中定义是 1000。

头文件中的clock函数:clock_t clock(), 这个函数返回从 “开启这个程序进程” 到 “程序中调用clock() 函数” 时之间的CPU时钟计时单元 (clock tick) 数。

waiting.cpp 代码如下:

 1 View Code 
 2  #include <iostream>
 3  #include <ctime>
 4  
 5  int main()
 6  {
 7      using namespace std;
 8      cout << "Enter the delay time. in seconds: ";
 9      float secs;
10      cin >> secs;
11      clock_t delay = secs * CLOCKS_PER_SEC;   
12      cout << "starting\a\n";                  
13      clock_t start = clock();
14      cout << delay << endl;
15  
16      while(clock() - start < delay)
17          cout << clock() - start << endl;;
18      cout << "done \a\n";
19  
20      return 0;
21  }
原文地址:https://www.cnblogs.com/akira90/p/2752634.html