头文件:
#ifdef _MSC_VER
#include<Windows.h>
#include <time.h>
#else
#include <time.h>
#include<sys/time.h>
#endif
INT64
GetTime_ms(void)
{
#ifdef _MSC_VER
LARGE_INTEGER m_nFreq;
LARGE_INTEGER m_nTime;
QueryPerformanceFrequency(&m_nFreq);
QueryPerformanceCounter(&m_nTime);
returnm_nTime.QuadPart *
1000 / m_nFreq.QuadPart;
#elif defined(__GNUC__)
structtimeval tv_date;
gettimeofday(&tv_date, NULL);
return(INT64)tv_date.tv_sec *
1000 + (INT64)tv_date.tv_usec;
#endif
}
定义三个时间参数:
INT32 time;
INT64 StartTime, EndTime;
使用:
StartTime = GetTime_ms();//在需要测试的代码前一行计算开始时间
EndTime = GetTime_ms();//结果代码的下一行计算结束时间
time = (INT32)(EndTime - StartTime);//计算测试代码的运行时间
printf("运行时间 = %d ms\n", time);
///////////////////////////////////////////////////////////////////////////
附:项目中使用到的一个性能测试类
#ifndef _PERFORMANCE_TEST_
#define _PERFORMANCE_TEST_
#if (!defined(_WIN32) &&!defined(_WIN32_WCE))
#include <string.h>
#include <stdio.h>
#include <linux/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <sys/times.h>
#else
#ifdef _WIN32_WCE
#else
#include <sys/Timeb.h>
#endif
#include <winsock2.h>
#include <time.h>
#endif //(!defined(_WIN32) && !defined(_WIN32_WCE))
#include "SNDataTypeDefine.h"
/************************************************************************
* 概述:性能测试类.
* 功能说明:
* 用于需要进行性能测试的地方.可以对代码块进行性能测试,输出该代码段所消耗的时间以及cpu占用率等信息.
* 使用范例:
* PerformanceTest objPerformanceTest;
* objPerformanceTest.begin();
//读取测试起始时间
* testMethod();
//在此处添加待测试的方法
* objPerformanceTest.end();
//读取测试结束时间
* objPerformanceTest.printWasteTime();
//打印测试使用时间,以us为单位
* objPerformanceTest.printCpuTime();
//打印Cpu使用时间,包括用户时间和系统时间及其占用比率
************************************************************************/
class PerformanceTest
{
public:
PerformanceTest();
~PerformanceTest();
public:
/************************************************************************
* 概述:启动测试.
* 输入:
* 无
* 输出:
* 无
* 返回值:
* 无
* 功能描述:
* 在测试程序调用前先调用该方法以记录测试启动时间.
************************************************************************/
void begin();
/************************************************************************
* 概述:结束测试.
* 输入:
* 无
* 输出:
* 无
* 返回值:
* 无
* 功能描述:
* 在测试程序后调用该方法以记录测试结束时间.
************************************************************************/
void end();
/************************************************************************
* 概述:打印测试程序消耗时间.精确到微秒(us)
* 输入:
* 无
* 输出:
* 无
* 返回值:
* 无
* 功能描述:
* 打印测试程序消耗时间.主要用于测试算法,计算耗时多少.
************************************************************************/
void printWasteTime();
INT64 getWasteTime();
/************************************************************************
* 概述:打印CPU时间.
* 输入:
* 无
* 输出:
* 无
* 返回值:
* 无
* 功能描述:
* 打印Cpu使用时间,包括用户时间和系统时间及其占用比率.主要用于测试耗时较长的功能.
************************************************************************/
void printCpuTime();
private:
#if (!defined(_WIN32) && !defined(_WIN32_WCE))
tms m_tStartTime;
//开始时间
tms m_tEndTime;
//结束时间
clock_t m_fStartTime;
//开始时间
clock_t m_fEndTime;
//结束时间
long clktck;
//时钟,每秒钟几次
timeval m_tvStartTime;
//开始时间
timeval m_tvEndTime;
//结束时间
#else
LARGE_INTEGER
m_Performancefrequency;
unsigned __int64
m_startTime;
unsigned __int64
m_endTime;
#endif //(!defined(_WIN32) && !defined(_WIN32_WCE))
};
#endif //_PERFORMANCE_TEST_
使用:
PerformanceTest performanceTest;
performanceTest.begin();
performanceTest.end();
int wasteTime = (int)(performanceTest.getWasteTime()/1000);