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

C++获取当前时间和计算程序运行时间的方法 
获取当前时间: 
#include <iostream> 
#include <Windows.h>  

using namespace std;  

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.h>
#include<time.h>
void main()
{
   clock_t start,finish;
   double totaltime;
   start=clock();

   …… //把你的程序代码插入到这里面

   finish=clock();
   totaltime=(double)(finish-start) / CLOCKS_PER_SEC;  //CLOCKS_PER_SEC注意一下
   cout<<"\n此程序的运行时间为"<<totaltime<<"秒!"<<endl;
}

计算程序运行时间 方法二: #include <iostream> #include <Windows.h> //关键 using namespace std; int main() { LONGLONG start, finish; LONGLONG totalTime; start = GetTickCount(); //需要测试运行时间的代码段放在这 finish = GetTickCount(); totalTime = finish - start; cout<<"花费"<<totalTime<<"毫秒"<<endl; return 0; }

  

posted @ 2015-04-16 16:47  Jeremy Wu  阅读(1624)  评论(0编辑  收藏  举报