您的位置:首页 > 编程语言 > C语言/C++

C++获取当前时间和计算程序运行时间的方法

2015-05-07 12:36 645 查看
C++获取当前时间和计算程序运行时间的方法
获取当前时间:
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: