如何获取代码运行时间

在调试中,经常需要计算某一段代码的执行时间,下面给出两种常用的方式:

第一种:使用GetTickCount函数

#include<iostream>
#include<windows.h>

int main()
{
    DWORD start_time=GetTickCount();
    {
        //此处为被测试代码
    }
    DWORD end_time=GetTickCount();
    cout<<"The run time is:"<<(end_time-start_time)<<"ms!"<<endl;//输出运行时间
    return 0;
   GetTickCount函数返回从系统运行到现在所经历的时间(类型为DWORD),单位为ms,因为DWORD表示范围的限制,所以使用此种方法存在限制,即系统的运行时间的ms表示不能超出DWORD的表示范围。

第二种:使用clock()函数 
# include<iostream>
#include<time.h>

int main()
{
    clock_t start_time=clock();

    {
        //被测试代码
    }

    clock_t end_time=clock();
    cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
    return 0;
}  
  clock_t,clock()定义于time.h中,clock()返回从程序运行时刻开始的时钟周期数,类型为long.CLOCKS_PER_SEC定义了每秒钟包含多少了时钟单元数,因为计算ms,所以*1000。
    
    由上面分析可知,用clock()函数计算运行时间,表示范围一定大于GetTickCount()函数,所以,建议使用clock()函数。


===========================================================================================================


1.使用CTime类 
CString str;
//获取系统时间
CTime tm;
tm=CTime::GetCurrentTime();
str=tm.Format("现在时间是%Y年%m月%d日 %X");
MessageBox(str,NULL,MB_OK);
2: 得到系统时间日期(使用GetLocalTime)

SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);
3.使用GetTickCount
//获取程序运行时间
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
Sleep(500);
long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)
str.Format("time:%dms",t2-t1);//前后之差即 程序运行时间
AfxMessageBox(str);
//获取系统运行时间
long t=GetTickCount();
CString str,str1;
str1.Format("系统已运行 %d时",t/3600000);
str=str1;
t%=3600000;
str1.Format("%d分",t/60000);
str+=str1;
t%=60000;
str1.Format("%d秒",t/1000);
str+=str1;
AfxMessageBox(str);

如何在VC6.0中得到一个程序的运行时间,也就是这个程序耗费的时钟周期数// C和C++的时间编程 
#include<iostream> 
#include<ctime> 
using namespace std; 
int main() 

time_t begin,end; 
begin=clock(); 
//这里加上你的代码 
end=clock(); 
cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl; 
}   
   

unix时间相关,也是标准库的
这些在<time.h>
1.timegm函数只是将struct tm结构转成time_t结构,不使用时区信息;
time_t timegm(struct tm *tm); 
2.mktime使用时区信息
time_t mktime(struct tm *tm);
timelocal 函数是GNU扩展的与posix函数mktime相当
time_t timelocal (struct tm *tm);
3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息;
struct tm * gmtime(const time_t *clock);
4.localtime使用时区信息
struct tm * localtime(const time_t *clock);
1.time获取时间,stime设置时间
time_t t;
t = time(&t);
2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;
int stime(time_t *tp)
3.UTC=true 表示采用夏时制;
4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;
5.设置时区推荐使用setup来设置;
6.设置时区也可以先更变/etc/sysconfig/clock中的设置 再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效
time_t只能表示68年的范围,即mktime只能返回1970-2038这一段范围的time_t
看看你的系统是否有time_t64,它能表示更大的时间范围

Window里面的一些不一样的
CTime MFC类,好像就是把time.h封了个类,没扩展
CTime t = GetCurrentTime();
SYSTEMTIME 结构包含毫秒信息
typedef struct _SYSTEMTIME { 
WORD wYear; 
WORD wMonth; 
WORD wDayOfWeek; 
WORD wDay; 
WORD wHour; 
WORD wMinute; 
WORD wSecond; 
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
SYSTEMTIME t1;
GetSystemTime(&t1) 
CTime curTime(t1); 
WORD ms = t1.wMilliseconds;
SYSTEMTIME sysTm;
::GetLocalTime(&sysTm);

在time.h中的_strtime() //只能在windows中用
char t[11];
_strtime(t);
puts(t);
------------------------------------------------------------------------------
_timeb定义在SYS\TIMEB.H,有四个fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
   _ftime( &timebuffer );
取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!

-------------------------------------------------------------------------
如何设定当前系统时间---windows
SYSTEMTIME m_myLocalTime,*lpSystemTime;
    m_myLocalTime.wYear=2003;
    m_myLocalTime.wMonth=1;
    m_myLocalTime.wDay=1;
    m_myLocalTime.wHour=0;
    m_myLocalTime.wMinute=0;
    m_myLocalTime.wSecond=0;
    m_myLocalTime.wMilliseconds=0;
    lpSystemTime=&m_myLocalTime;
    if( SetLocalTime(lpSystemTime) ) //此处换成 SetSystemTime( )也不行
       MessageBox("OK !"); 
    else
       MessageBox("Error !"); 

SYSTEMTIME m_myLocalTime,*lpSystemTime;
m_myLocalTime.wYear=2003;
m_myLocalTime.wMonth=1;
m_myLocalTime.wDay=1;
lpSystemTime=&m_myLocalTime;
if( SetDate(lpSystemTime) ) //此处换成 SetSystemTime( )也不行
    MessageBox("OK !"); 
else
    MessageBox("Error !"); 

-----------------------------------------------------------------------------
用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。
clock_t clock ( void );
#include <time.h>
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是记录时钟周期的,实现看来不会很精确,需要试验验证;
---------------------------------------------------------------------------
据说tc2.0的time结构含有毫秒信息
#include   <stdio.h> 
#include   <dos.h>
int main(void) 

   struct time t;
   gettime(&t); 
   printf("The current time is: %2d:%02d:%02d.%02d\n", 
          t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); 
   return 0; 

time 是一个结构体,, 其中成员函数 ti_hund 是豪秒。。。上程序可以在tc2.0运行
--------------------------------------------------------------------------------
这个是windows里面常用来计算程序运行时间的函数;
DWORD dwStart = GetTickCount();
//这里运行你的程序代码
DWORD dwEnd = GetTickCount();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
这个函数只精确到55ms,1个tick就是55ms。

--------------------------------------------------------------------------------
timeGetTime()基本等于GetTickCount(),但是精度更高
DWORD dwStart = timeGetTime();
//这里运行你的程序代码
DWORD dwEnd = timeGetTime();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位 
虽然返回的值单位应该是ms,但传说精度只有10ms。
--------------------------------------------------------------------------------

Borland C++ Builder VCL的时间函数
  1. Date
  返回TDateTime对象,包含当前的年月日信息,函数原型如下:
  System::TDateTime __fastcall Date(void);
  2. Time
  返回TDateTime对象,包含当前的时间信息,函数原型如下:
  System::TDateTime __fastcall Time(void);
  3. Now
  返回TDateTime对象,获取当前的日期和时间信息,函数原型如下:
  System::TDateTime __fastcall Now(void);
  4. DatetimeToString
  将TDateTime对象转换为指定格式的字符串对象,函数原型如下:
  void __fastcall DateTimeToString(AnsiString &;Result, const AnsiString Format,System::TDateTime DateTime);
  5. DateToStr
  将TDateTime对象(包含当前年月日信息)转换为字符串对象,函数原型如下:
  AnsiString __fastcall DateToStr(System::TDateTime Date);
  6. TimeToStr
  将当前日期转换为字符串对象,函数原型如下:
  AnsiString __fastcall TimeToStr(System::TDateTime Time);
  7. DateTimetoStr
  将TDateTime对象转换为字符串对象,函数原型如下:
  AnsiString __fastcall DateTimeToStr(System::TDateTime DateTime);
  8. StrToDate
  将字符串对象转换为年月日对象,函数原型如下:
  System::TDateTime __fastcall StrToDate(const AnsiString S);
  9. StrToTime
  将字符串对象转换时间对象,函数原型如下:
  System::TDateTime __fastcall StrToTime(const AnsiString S);
  10.StrToDateTime
  将字符串对象转换为年月日时间对象,函数原型如下:
  System::TDateTime __fastcall StrToDateTime(const AnsiString S);
  11.DateTimeToSystemTime
  将TDateTime对象转换为操作系统时间,函数原型如下:
  void __fastcall DateTimeToSystemTime(System::TDateTime DateTime, _SYSTEMTIME &;SystemTime);
  12.SystemTimeToDateTime
  将操作系统时间转换为TDateTime对象,函数原型如下:
  System::TDateTime __fastcall SystemTimeToDateTime(const _SYSTEMTIME &;SystemTime);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值