关于clock()计算进程运行的时间以及time()


1.

  • C++ clock()函数实现功能介绍

    我们今天要向大家介绍的是有关C++ clock()函数的一些基本概念以及应用方式,目的就是希望能够帮助大家轻松掌握这一应用技巧。

     

    C++编程语言中,有许多特定的函数应用可以帮助我们实现许多不同的特定功能,方便程序员的实际开发。比如C++ clock()函数主要是帮助我们实现计时的功能。而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:

    1. clock_t clock( void );  

    这个函数返回从“开启这个程序进程”到“程序中调用C++ clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义:

    1. #ifndef _CLOCK_T_DEFINED   
    2. typedef long clock_t;   
    3. #define _CLOCK_T_DEFINED   
    4. #endif  

    很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

    1. #define CLOCKS_PER_SEC ((clock_t)1000) 

    可以看到每过千分之一秒(1毫秒),调用C++ clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

    1. void elapsed_time()   
    2. {   
    3. printf("Elapsed time:%u secs."n",clock()/CLOCKS_PER_SEC);   

    当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:

    1. #include “stdio.h”   
    2. #include “stdlib.h”   
    3. #include “time.h”   
    4. int main( void )   
    5. {   
    6. long i = 10000000L;   
    7. clock_t start, finish;   
    8. double duration;   
    9. /* 测量一个事件持续的时间*/   
    10. printf( "Time to do %ld empty loops is ", i );   
    11. start = clock();   
    12. while( i-- ) ;   
    13. finish = clock();   
    14. duration = (double)(finish - start) / CLOCKS_PER_SEC;   
    15. printf( "%f seconds"n", duration );   
    16. system("pause");   

    在笔者的机器上,运行结果如下:

    1. Time to do 10000000 empty loops is 0.03000 seconds  

    以上就是对C++ clock()函数的相关应用介绍。上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一 些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。

2.time_t time()
struct tm;
3.
dword getTickCount()
dword timeGettime() 两个API。
0 到 49.7天

一. 基本概念:
1.1 时间的分类
本地时间
协调世界时间(Coordinated Universal Time ,UTC).也即我们常常说的格林威治时 间.
格林威治时间与本地时间的差值,也就是我们通常说的时差.由于我们这边是北京时间(也称东八区).所有差值是8.

1.2 常用类(结构)
CRT提供的时间
time_t:是ong型.我们一般无法理解这个值表示的意义.必须要进行转化才行.
tm结构定义如下:
struct tm

int tm_sec; /* seconds after the minute - [0,59] */ 
int tm_min; /* minutes after the hour - [0,59] */ 
int tm_hour; /* hours since midnight - [0,23] */ 
int tm_mday; /* day of the month - [1,31] */ 
int tm_mon; /* months since January - [0,11] */ 
int tm_year; /* years since 1900 */ 
int tm_wday; /* days since Sunday - [0,6] */ 
int tm_yday; /* days since January 1 - [0,365] */ 
int tm_isdst; /* daylight savings time flag */ 
};
这个结构我们看起来就比较容易理解了.
如果得到time_t格式的时间,我们可以利用localtime来进行转化.
如果想要进行反向转化(即tm->time_t)就需要使用
time_t mktime(struct tm* timeptr);
另外如果想要将其转化为UTC对应的时间,则需要使用
errno_t gmtime_s(struct tm* _tm,const time_t* time);

SDK时间.上述两类时间是针对CRT,下面是对应的SDK的两个等价时间

typedef struct _FILETIME

DWORD dwLowDateTime; 
DWORD dwHighDateTime; 
} FILETIME, *PFILETIME;
对应于time_t

typedef struct _SYSTEMTIME

WORD wYear; 
WORD wMonth; 
WORD wDayOfWeek; 
WORD wDay; 
WORD wHour; 
WORD wMinute; 
WORD wSecond; 
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
对应于tm,但是这里多了最后一项(说明更加精确了)

1.3 常用的一些获取及其转化函数
● 获取UTC时间:
Void GetSystemTime( LPSYSTEM lpSystemTime);
● 获取本地时间(即北京时间)
Void GetLocalTime(LPSYSTEM lpSystemTime);
● FILETIME 转为SYSTEMTIME函数
BOOL SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime,
LPFILETIME lpFIleTime);
● SYSTEMTIME转为FILETIME函数
BOOL FileTimeToSystemTime(const FILETIME* lpFileTime,
LPSYSTEMTIME lpSystem);
另外也提供UTC和本地时间互转的两个函数
BOOL LocalFileTimeToFileTime( const FILETIME* lpLocalFileTime,
LPFILETIME lpFileTime);
BOOL FileTimeToLocalFileTime(const FILETIME* lpFileTime,
LPFILETIME lpLocalFileTime);
最后一个就是ULARGE_INTEGER结构,这个结构的定义如下:
typedef union ULARGE_INTEGER {
struct {
DWORD LowPart;
DWORD HighPart;
};
struct {
DWORD LowPart;
DWORD HighPart;
} u;
ULONGLONG QuadPart;
} ULARGE_INTEGER;
这个结构和FILETIME没有很大差别,主要是前者32为对齐,后者64位对齐,若要进行转化,则必须要进行赋值操作.
一些简单的参考代码:
CTime ct = CTime::GetCurrentTime();
//
time_t tt = ct.GetTime();
//
tm *pt = localtime( &tt );
// CString strTime;
// strTime.Format( "%d-%d-%d %d:%d:%d",pt->tm_year + 1900,pt->tm_mon + 1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec );
// MessageBox( strTime );
// SYSTEMTIME stUTC,stLocal;
// GetSystemTime( &stUTC );
// GetLocalTime( &stLocal );

HANDLE hFile = CreateFile( "C:\\ysl.txt",GENERIC_READ|GENERIC_WRITE, 0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_READONLY,NULL );
FILETIME ftCreate,ftAccess,ftWrite,ftLocal;
SYSTEMTIME st;

GetFileTime( hFile,&ftCreate,&ftAccess,&ftWrite );
FileTimeToLocalFileTime( &ftCreate,&ftLocal );
FileTimeToSystemTime( &ftLocal,&st );
ULARGE_INTEGER uli;
uli.HighPart = ftLocal.dwHighDateTime;
uli.LowPart = ftLocal.dwLowDateTime;

CString strTime;
strTime.Format( "%d",uli.QuadPart );
MessageBox( strTime );
/*
strTime.Empty();
strTime.Format( "%d-%d-%d %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond );
MessageBox( strTime );
*/

原文链接: https://www.cnblogs.com/peterjade/archive/2011/05/23/2428504.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    关于clock()计算进程运行的时间以及time()

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/25979

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午3:44
下一篇 2023年2月8日 上午3:45

相关推荐