C/C++中系统时间的实现
来源:互联网 发布:linux 内核调试kgd 编辑:程序博客网 时间:2024/06/08 07:33
怎么在程序中,实现让程序计时呢?也就是说怎么让程序等待我想要的秒数?可以使用C/C++提供的系统函数clock()实现:
ANSI C和C++中的库函数中有一个叫做clock()的函数,专门用来实现计时的。但是clock()不能返回会秒数,因为秒数跟十进制一样是人类的计时方法,计算机中是使用计算机硬件的系统时钟单位来计时的。该函数返回的值可能是一个long型的,也有可能是unsigned long型的,那么怎么才能实现可移植性呢?其实,在time.h(C++:ctime)中定义了一个符号常量——CLOCKS_PER_SEC,该常量等于每秒包含的系统时间单位数。因此可以如下实现:
C语言代码:
#include <stdio.h>#include <time.h>int main (void){float sec;clock_t delay;clock_t start;printf ("Please enter the delay seconds!\n");scanf ("%f",&sec);delay = sec * CLOCKS_PER_SEC;printf ("Starting\a\n");start = clock();while (clock() - start < delay);return 0;}C++代码:
#include <iostream>#include <ctime>int main (void){using namespace std;float sec;clock_t delay;clock_t start;cout << "Please enter the delay seconds:" << endl;cin >> sec;delay = sec * CLOCKS_PER_SEC;cout << "Starting\a\n";start = clock();while (clock() - start < delay);cout << "done!\a\n";return 0;}
- C/C++中系统时间的实现
- c语言中时间陷阱的实现
- c中得到系统时间
- C语言中获取系统时间的方法
- c语言获取系统时间 程序实现
- C/C++中获取系统时间
- C语言中获取当前系统时间
- C中获取当前系统时间
- C语言中如何获取系统时间?
- [C] C语言中获得系统当前日期 和 时间
- [C-C++]获取系统时间
- C语言实现判断闰年、从常规时间到卫星系统时间的转换
- C 取得系统时间!
- C++builder 系统时间
- 【C++】获取系统时间
- C显示系统时间
- C语言系统时间
- linux c 获取系统时间的方法
- POJ 3905 - Perfect Election 简单2-sat
- Linux下,Weblogic开机启动
- Dev c++ No rule to make target问题解决
- 小Q系列故事——最佳裁判
- SQL语句大全
- C/C++中系统时间的实现
- 3.2 Fragment的生命周期
- 大数据分析架构中需要权衡的四个因素
- 解决在eclipse中导入项目名称已存在的问题
- 01. 搭配飞行员 [网络流24题]
- 应用中弹出 WiFi 提示框的方法
- PspTerminateProcess 结束冰刃进程
- OpenCV 支持向量机(SVM)介绍
- Prime Ring Problem