标准C/C++计算程序运行时间的函数和类型介绍

我们在进行编程的时候, 不免要测试程序的运行时间, 下面, 将介绍一些测试程序运行时间的方法.

时间的表示方法

    Coordinated Universal Time:

        协调世界时, 又称为统一时间, 是世界的标准时间, 简称为UTC.

这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协调世界时在互联网中使用的一种方式。

在军事中,协调世界时区会使用"Z"来表示。又由于Z在无线电联络中使用"Zulu"作代称,协调世界时也会被称为"Zulu time"。

中国大陆、中国香港、中国澳门、中国台湾、蒙古国、新加坡、马来西亚、菲律宾、西澳大利亚州的时间与UTC的时差均为+8,也就是UTC+8。

    国际原子时(TAI)

        国际原子时(TAI)是根据以下秒的定义的一种国际参照时标, 属于国际单位制(SI)。

1967年第13届国际度量衡会议上通过一项决议,定义一秒为铯-133原子基态两个超精细能级间跃迁辐射9,192,631,770周所持续的时间。

    格林尼治标准时(GMT)

        是指位于英国伦敦郊区的皇家格林威治天文台的标准时间,因为本初子午线被定义在通过那里的经线。自1924年2月5日开始,格林威治天文台每隔一小时会向全世界发放调时信息。

当然, 除此之外还有太阳时, 世界时, 历书时等时间标准.

标准C/C++时间测量相关函数

相关函数和类型

    /*
 * Number of clock ticks per second. A clock tick is the unit by which
 * processor time is measured and is returned by 'clock'.
 */
#define CLOCKS_PER_SEC  ((clock_t)1000)
#define CLK_TCK     CLOCKS_PER_SEC

/*
    
函数返回自程序开始运行的处理器时间如果无可用信息返回-1. 
    
返回的是按照1/CLOCKS_PER_SECOND为单位进行计数的.
*/
clock_t clock( void );

    举例

        代码

#include <time.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void doFunc()
{
    for(int i = 0; i < 100000; i++)
        for(int j = 0; j < 10000; j++)
            i+j;
}
int main()
{
    clock_t start = clock();
    doFunc();
    clock_t finish = clock();

    cout << "start :\t\t" << (double)start/CLOCKS_PER_SEC << 's' << endl;
    cout << "finish:\t\t" << (double)finish/CLOCKS_PER_SEC << 's' << endl;

    cout << "finish-start:\t" << (double)(finish - start) / CLOCKS_PER_SEC << 's' << endl;

    return 0;
}

        输出

            

            

            

分解时间

    类型和函数介绍

    /*
Members
    tm_sec 
        Contains a value between 0 and 59 that indicates the number of seconds. 
    tm_min 
        Contains a value between 0 and 59 that indicates the number of hours. 
    tm_hour 
        Contains a value between 0 and 23 that indicates the number of hours since midnight. 
    tm_mday 
        Contains a value between 1 and 31 that indicates the day of the month. 
    tm_mon 
        Contains a value between 0 and 11 that indicates the number of months since January. 
    tm_year 
        Indicates the number of years since 1900. 
    tm_wday 
        Contains a value between 0 and 6 that indicates the number of days since Sunday. 
    tm_yday 
        Contains a value between 0 and 365 that indicates the number of days since January 1. 
    tm_isdst 
        Indicates daylight savings time when TRUE and normal time when FALSE. 
Headers
    Declared in hbaapi.h. Include hbaapi.h.
*/
struct tm {
  int  tm_sec;
  int  tm_min;
  int  tm_hour;
  int  tm_mday;
  int  tm_mon;
  int  tm_year;
  int  tm_wday;
  int  tm_yday;
  int  tm_isdst;
};

/*
time_t
    
表示的时间(日历时间)是从一个时间点(例如:197011000秒)到此时的秒数
问题:
    
超出范围的时候就会产生溢出这是在windows下的表示我们发现他将会在7083080823.7年溢出所以使用该种类型的无需担心溢出
    
*/
# ifndef __int64
#  define __int64 long long
typedef __int64 __time64_t;     /* 64-bit time value */
typedef __time64_t time_t;      /* time value */


/*
Parameters
    timer
        Pointer to stored time. The time is represented as seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).
Return Value
    Return a pointer to a structure of type tm. The fields of the returned structure 
    hold the evaluated value of the timer argument in UTC rather than in local time. 
    Each of the structure fields is of type int.
Required header
    <time.h>
*/

 struct tm *gmtime( const time_t *timer);


/*
Parameters
    timer
        Pointer to stored time.
Return Value
    Return a pointer to the structure result, or NULL if the date passed to the function is:
        Before midnight, January 1, 1970.
        After 03:14:07, January 19, 2038, UTC (using _time32 and time32_t).
        After 23:59:59, December 31, 3000, UTC (using _time64 and __time64_t).
Remarks
    
函数返回给定的统一世界时间(通常是格林威治时间),如果系统不支持统一世界时间系统返回NULL
    The localtime function converts a time stored as a time_t value and stores the result in a 
    structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, UTC. This value is usually obtained from the time function.
    Both the 32-bit and 64-bit versions ofgmtime, mktime, mkgmtime, and localtimeall use a single tm 
    structure per thread for the conversion. Each call to one of these routines destroys the result of the previous call.
    localtime corrects for the local time zone if the user first sets the global environment variable TZ.
    When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. If the TZ variable is not set, localtime attempts to use the time zone information specified in the Date/Time application in Control Panel. If this information cannot be obtained, PST8PDT, which signifies the Pacific Time Zone, is used by default. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.


*/
struct tm *localtime( const time_t *time );

/*
函数将ptr所指向的时间结构转换成下列字符串:
    day month date hours:minutes:seconds year\n\0
例如:
    Mon Jun 26 12:03:53 2000
*/
_CRTIMP char__cdecl __MINGW_NOTHROW       asctime (const struct tm* ptr);

    举例

        代码

    #include <time.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    struct tm* newtime;
    time_t ltime;

    time(&ltime);
    cout << "ltime:\t" << ltime << endl;   //
输出UTC时间
    newtime = gmtime(&ltime);              //
格式化UTC时间为GMT
    cout << asctime(newtime);            //
GMT时间转换字符串并输出

    return 0;
}

        输出

            

补充

        当然, 还有其他测试方法, 在这里值介绍最通用的, 值得注意的是, 这里获取时间是在同一个线程中进行的, 所以, 计算程序运行时间上, 会稍微多一些, 如果使用多线程

参考文档:

  1. 百度百科—Coordinated Universal Time
  2. wikipedia—原子时
  3. wikipedia—格林尼治标准时
  4. 参见MSDN

    

    

posted @ 2013-12-27 00:46  黑色包裹  阅读(1546)  评论(0编辑  收藏  举报