clock函数方式
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);
clock_gettime函数方式
int clock_gettime(clockid_t clk_id, struct timespec *tp);
The functions clock_gettime() retrieve the time of the specified clock clk_id.
The clk_id argument is the identifier of the particular clock on
which to act. A clock may be system-wide and hence visible for
all processes, or per-process if it measures time only within a
single process.
All implementations support the system-wide real-time clock,
which is identified by CLOCK_REALTIME. Its time represents sec‐
onds and nanoseconds since the Epoch. When its time is changed,
timers for a relative interval are unaffected, but timers for an
absolute point in time are affected.
More clocks may be implemented. The interpretation of the cor‐
responding time values and the effect on timers is unspecified.
Sufficiently recent versions of glibc and the Linux kernel sup‐
port the following clocks:
CLOCK_REALTIME
System-wide real-time clock. Setting this clock requires
appropriate privileges.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time
since some unspecified starting point.
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
Similar to CLOCK_MONOTONIC, but provides access to a raw
hardware-based time that is not subject to NTP adjust‐
ments.
CLOCK_PROCESS_CPUTIME_ID
High-resolution per-process timer from the CPU.
CLOCK_THREAD_CPUTIME_ID
Thread-specific CPU-time clock.
具体实现
#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;
};
测试执行效率
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
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;
};
int main(int argc, char** argv)
{
const int iLoop = 100000000;
TimeTool tool;
tool.begin();
struct timeval stick0;
for (int i = 0; i < iLoop; ++i)
{
gettimeofday(&stick0, NULL);
}
tool.end();
printf("gettimeofday 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("clock cost %f\n", tool.getInterval());
tool.reset();
tool.begin();
struct timespec stick2;
for (int i = 0; i < iLoop; ++i)
{
clock_gettime(CLOCK_MONOTONIC, &stick2);
}
tool.end();
printf("clock_gettime cost %f\n", tool.getInterval());
}
执行测试可以发现gettimeofday函数的执行效率远高于clock函数
注:发现该测试结论和环境有些相关,有时候gettimeofday cost需要近1分钟,有时候却只需要2秒左右(只是重启了一下机器)
$ g++ -O2 t.cpp -lrt$ ./a.out
gettimeofday cost 2.288930
clock cost 20.730558
clock_gettime cost 2.097753
$ ./a.out
gettimeofday cost 1.291197
clock cost 20.868483
clock_gettime cost 2.111481
$ ./a.out
gettimeofday cost 2.303097
clock cost 20.924046
clock_gettime cost 1.121241
PS: 在libevent的gettime函数中有用到clock_gettime或者gettimeofday方式(通过宏和标志位来决定采用哪一个)