[C++]C++中的時間

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

C++中的時間

#include <stdio.h>

#include <tchar.h>

#include <string>

#include <iostream>

#include <time.h>

#include <conio.h>

#include <dos.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

clock_t start,end;

//宏定義:__DATE__、__TIME__、__FILE__、__LINE__,表示在代碼編譯時的信息。

string dateCode = __DATE__; //文件編譯時的日期。

string timeCode = __TIME__; //文件編譯時的時間。

cout<<"Creat the software of date:"<<dateCode<<endl;

cout<<"Creat the software of time:"<<timeCode<<endl;

start = clock(); //程序運行到這裡開始統計CPU運行時間,此時值為0。

_getch(); //按下任意一鍵時程序繼續執行。

end = clock(); //按下鍵後統計時間。

cout<<"Press any key to continue"<<endl;

//由於end和start為毫秒,故除以CLK_TCK轉為秒值。

cout<<"Program run time: "<<(end - start)/CLK_TCK<<" Sceond."<<endl;

return 0;

}

可以用clock來統計程序運行的時間長短。

time返回从1970年1月1日0时到现在的秒数,即系统时间。

clock是自进程启动后此进程使用cpu的总毫秒数,常用来测试任务执行的速度。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航