C++ 获取时间间隔
2013-04-26 10:00
295 查看
clock函数方式
Linux平台下C/C++中获取时间间隔的方法,一种比较普遍的认识是采用clock函数
Returns the number of clock ticks elapsed since the program was launched.
The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).
The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call
to clock.
通过两次调用clock函数可以得到起始时间和结束时间,从而得到毫秒级的时间差
gettimeofday函数方式
C/C++语言中有个timeval结构是可以精确到毫秒,可以利用timeval来记录起始时间和结束时间
struct timeval {
time_t tv_sec; /* seconds */
SUSEconds_t tv_usec; /* microseconds */
};
通过调用gettimeofday函数可以得到用timeval结构记录的当前时间
int gettimeofday(struct timeval *tv, struct timezone *tz);
具体实现
#include <time.h>
#include <sys/time.h>
class ClockTool
{
public:
ClockTool()
: m_begin(0), m_end(0)
{
}
inline void begin()
{
m_begin = clock();
}
inline void end()
{
m_end = clock();
}
inline void reset()
{
m_begin = 0;
m_end = 0;
}
float getInterval()
{
if (m_end < m_begin)
{
return 0;
}
return (double)(m_end - m_begin)/CLOCKS_PER_SEC;
}
private:
clock_t m_begin;
clock_t m_end;
};
class TimeTool
{
public:
TimeTool()
{
reset();
}
inline void begin()
{
gettimeofday(&m_begin, NULL);
}
inline void end()
{
gettimeofday(&m_end, NULL);
}
inline void reset()
{
memset(&m_begin, 0, sizeof(struct timeval));
memset(&m_end, 0, sizeof(struct timeval));
}
float getInterval()
{
if (m_end.tv_usec < m_begin.tv_usec)
{
m_end.tv_usec += 1000;
m_end.tv_sec = m_end.tv_sec - 1;
}
return (m_end.tv_sec - m_begin.tv_sec) + (m_end.tv_usec - m_begin.tv_usec) / 1000000.0;
}
private:
struct timeval m_begin;
struct timeval m_end;
};
测试clock函数与gettimeofday函数的执行效率
#include <stdio.h>
#include <string.h>
#include "clocktool.h"
int main(int argc, char** argv)
{
const int iLoop = 100000000;
TimeTool tool;
tool.begin();
struct timeval stick0;
for (int i = 0; i < iLoop; ++i)
{
memset(&stick0, 0, sizeof(struct timeval));
gettimeofday(&stick0, NULL);
}
tool.end();
printf("cost %f\n", tool.getInterval());
tool.reset();
tool.begin();
clock_t stick1= 0;
for (int i = 0; i < iLoop; ++i)
{
stick1 = clock();
}
tool.end();
printf("cost %f\n", tool.getInterval());
}
执行测试可以发现gettimeofday函数的执行效率远高于clock函数
注:发现该测试结论和环境有些相关,有些环境下两者差别没有这么大(并且编译时是否带优化选项-O2对clock的执行效率也影响比较大),比较接近,但总体而言gettimeofday仍优于clock
cost 2.469688
cost 26.162617
Linux平台下C/C++中获取时间间隔的方法,一种比较普遍的认识是采用clock函数
clock_t clock ( void );
Returns the number of clock ticks elapsed since the program was launched.
The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).
The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call
to clock.
通过两次调用clock函数可以得到起始时间和结束时间,从而得到毫秒级的时间差
gettimeofday函数方式
C/C++语言中有个timeval结构是可以精确到毫秒,可以利用timeval来记录起始时间和结束时间
struct timeval {
time_t tv_sec; /* seconds */
SUSEconds_t tv_usec; /* microseconds */
};
通过调用gettimeofday函数可以得到用timeval结构记录的当前时间
int gettimeofday(struct timeval *tv, struct timezone *tz);
具体实现
#include <time.h>
#include <sys/time.h>
class ClockTool
{
public:
ClockTool()
: m_begin(0), m_end(0)
{
}
inline void begin()
{
m_begin = clock();
}
inline void end()
{
m_end = clock();
}
inline void reset()
{
m_begin = 0;
m_end = 0;
}
float getInterval()
{
if (m_end < m_begin)
{
return 0;
}
return (double)(m_end - m_begin)/CLOCKS_PER_SEC;
}
private:
clock_t m_begin;
clock_t m_end;
};
class TimeTool
{
public:
TimeTool()
{
reset();
}
inline void begin()
{
gettimeofday(&m_begin, NULL);
}
inline void end()
{
gettimeofday(&m_end, NULL);
}
inline void reset()
{
memset(&m_begin, 0, sizeof(struct timeval));
memset(&m_end, 0, sizeof(struct timeval));
}
float getInterval()
{
if (m_end.tv_usec < m_begin.tv_usec)
{
m_end.tv_usec += 1000;
m_end.tv_sec = m_end.tv_sec - 1;
}
return (m_end.tv_sec - m_begin.tv_sec) + (m_end.tv_usec - m_begin.tv_usec) / 1000000.0;
}
private:
struct timeval m_begin;
struct timeval m_end;
};
测试clock函数与gettimeofday函数的执行效率
#include <stdio.h>
#include <string.h>
#include "clocktool.h"
int main(int argc, char** argv)
{
const int iLoop = 100000000;
TimeTool tool;
tool.begin();
struct timeval stick0;
for (int i = 0; i < iLoop; ++i)
{
memset(&stick0, 0, sizeof(struct timeval));
gettimeofday(&stick0, NULL);
}
tool.end();
printf("cost %f\n", tool.getInterval());
tool.reset();
tool.begin();
clock_t stick1= 0;
for (int i = 0; i < iLoop; ++i)
{
stick1 = clock();
}
tool.end();
printf("cost %f\n", tool.getInterval());
}
执行测试可以发现gettimeofday函数的执行效率远高于clock函数
注:发现该测试结论和环境有些相关,有些环境下两者差别没有这么大(并且编译时是否带优化选项-O2对clock的执行效率也影响比较大),比较接近,但总体而言gettimeofday仍优于clock
cost 2.469688
cost 26.162617
相关文章推荐
- C/C++ 获取时间间隔的方法
- Windows/Linux C++ 获取时间间隔(单位:s , ms)
- C++获取程序运行时间
- node 实现获取时间发生间隔
- C++获取当前系统时间
- 获取间隔时间
- c++获取windows时间的方法(转)
- C++获取系统时间
- C++ 获取程序运行时间/时间戳 精确到毫秒
- C/C++获取当前系统时间
- C++使用API获取当前时间
- C++中获取系统时间类似于Java中的System.currentTimeMillis()
- C++中获取时间的函数
- 获取时间间隔
- 如何利用C++的time头文件获取系统时间
- C/C++获取当前系统毫秒时间
- C++获取时间的问题
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- java根据时间date获取该时间距离现在的时间间隔