C++获取当前时间和计算程序运行时间的方法
获取当前时间:
注意一点就是如果程序很小,那么运行速度回很快,此时求出的程序运行时间会等于0
#include <iostream>
#include <Windows.h>
using namespacestd;
int main()
{
SYSTEMTIME sys;
GetLocalTime(&sys);
cout<<sys.wYear<<"年";
cout<<sys.wMonth<<"月";
cout<<sys.wDay<<"日";
cout<<sys.wHour<<"时";
cout<<sys.wMinute<<"分";
cout<<sys.wSecond<<"秒";
cout<<sys.wMilliseconds<<"毫秒";
cout<<",星期"<<sys.wDayOfWeek<<endl;
return 0;
}
计算程序运行时间 方法一:
#include <iostream>
#include <time.h>//关键
using namespacestd;
int main()
{
clock_t start, finish;
doubletotalTime;
start = clock();
//需要测试运行时间的代码段放在这
finish = clock();
totalTime = (double)(finish- start);
cout<<"花费"<<totalTime<<"毫秒"<<endl;
return 0;
}
计算程序运行时间 方法二:
#include <iostream>
#include <Windows.h>//关键
using namespacestd;
int main()
{
LONGLONG start, finish;
LONGLONG totalTime;
start = GetTickCount();
//需要测试运行时间的代码段放在这
finish = GetTickCount();
totalTime = finish - start;
cout<<"花费"<<totalTime<<"毫秒"<<endl;
return 0;
}
在C++中,我们可以使用clock()函数来返回调用程序运行时间量的近似值,一般单位是毫秒。但是,如果我们想要把它转换到秒的话,就需要将clock()的返回值除上CLOCKS_PER_SEC,这样就可以将时间转换成秒数。其中,CLOCKS_PER_SEC在vs2008中的定义为:
#define CLOCKS_PER_SEC 1000
下面,我们用一个程序来举例说明下。注意一点,要想使用clock()函数,我们必须包含time.h头文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
using namespace std;
#include <time.h>
void cost_time()
{
cout<< "调用该程序所花费的时间为:" << clock ()/CLOCKS_PER_SEC<< "秒\n" ;
cout<< "调用该程序所花费的时间为:" << clock ()<< "毫秒\n" ;
}
int main()
{
for ( int a=0;a!=1000; )
{
cout<< "a=" <<a<< ' ' ;
a=a+1;
}
cost_time();
return 0;
}
|
程序的运行结果为:(省略a从1到884哈)
=======================显示当前日期=======================
首先给出代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
using namespace std;
#include <time.h>
#include <locale.h>
int main()
{
setlocale (LC_TIME, "" );
time_t TIME;
struct tm *TM;
char ch[81],ch1[81];
time (&TIME);
TM= gmtime (&TIME);
strftime (ch,80, "%#x" ,TM);
strftime (ch1,80, "%x" ,TM);
cout<<ch<<endl;
cout<<ch1<<endl;
return 0;
}
|
程序输出如下:
这样,就可以输出当前日期了。其中要注意一下,程序14行中的"%#x"表示输出完整时间,而15行的"%x"表示输出简略时间。
C++获取当前时间和计算程序运行时间的方法相关推荐
- mysql从当前月向前推12_JavaScript获取当前时间向前推三个月的方法示例
本文实例讲述了JavaScript获取当前时间向前推三个月的方法.分享给大家供大家参考,具体如下: /p> "http://www.w3.org/TR/xhtml1/DTD/xhtml ...
- php记录当前毫秒,php获取当前时间的毫秒数的方法
php获取当前时间的毫秒数的方法 php本身没有提供返回毫秒数的函数,但提供了一个microtime()函数,该函数返回一个array,包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可 ...
- VC获取系统时间、程序运行时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是 ...
- 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- php获取时区,php获取当前时间及时区设置的方法详解【附视频】
本篇文章主要给大家介绍用PHP如何获取当前时间的两种方法和时区设置的方法,以及分别获取昨天和明天的当前时间方法. 下面我们就通过具体的PHP代码示例,来给大家详细解说. 一.通过PHP time函数直 ...
- java当前时间的时间戳_java获取当前时间(时间戳)的方法
获取当前时间戳(毫秒级) //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方 ...
- 计算程序运行时间(time_t, clock_t)
转载自:http://blog.chinaunix.net/uid-23208702-id-75182.html 计算程序运行时间(time_t, clock_t)-whyliyi-ChinaUnix ...
- linux 计算程序运行时间
写的全面的一篇 还有一种 int getitimer(int which, struct itimerval *value); int setitimer(int which, const struc ...
- linux获得系统时间 c,linux c 获取系统时间
#include main() { time_t timep; time (&timep); printf("%s",asctime(gmtime(&timep)) ...
最新文章
- 压缩感知的阶段性总结
- 树莓派(Raspberry Pi 3) centos7使用yum命令报错File /usr/bin/yum, line 30 except KeyboardInterrupt, e:...
- 酷狗kuGoo 2007 和 flex有冲突
- Linux netstat命令介绍
- xml文件 卷积神经网络_理解卷积神经网络中的输入与输出形状(Keras实现)
- (11) Hibernate 缓存机制
- Comparable、Iterator接口和Collections类的实现方法
- 《线程管理:线程基本操作》
- springboot项目 tomcat8.x 频繁宕机 原因分析
- python利用百度云接口实现车牌识别
- ubuntu ssh 登录日志_全球第一开源ERP Odoo操作手册 安装ssh服务和ssh客户端
- https://github.com/nostra13/Android-Universal-Image-Loader
- Java中队列的使用
- 关于SQL中的两个问题的理解
- Windows下使用mingw32
- 台式计算机可以连接蓝牙吗,台式电脑可以连接蓝牙音响吗
- 压力换算公斤单位换算_压力单位换算方法
- 心中无码便是高清,用“脑补”除马赛克!
- vtuber面部捕捉工具_如何做一名VTuber?一个VUP就足够
- CCF 202206-4 光线追踪 python
热门文章
- 第十五届全国大学生智能汽车竞赛 人工智能创意组总决赛
- 996+GPA+500
- python flask跨域_Ajax与Flask传值的跨域问题
- inject 响应式_vue 的 provide 和 inject 依赖注入与 $parent
- java 图片不更新,图片下传页面不能及时更新,求解
- 字符串函数用法 php,PHP字符串函数print()的用法
- 使用说明 vector_C++核心准则编译边学-F.20 输出结果时应该使用返回值
- union 中的注意事项
- adjango 基本的使用
- linux 32位redis安装,CentOS 5.5 32位上安装Redis 2.6报错解决