跨平台的timmer
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#ifndef _PERF_TIMER_H_
#define _PERF_TIMER_H_
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
class PerfTimer
{
public:
PerfTimer();
~PerfTimer();
void reset();
void start();
void stop();
const double duration()const;
const double duration_milli_seconds()const;
const double duration_micro_seconds()const;
private:
#ifdef _WIN32
__int64 m_start_time;
__int64 m_stop_time;
__int64 m_freq;
#else
struct timeval m_start_time;
struct timeval m_stop_time;
#endif
};
#endif
#define _PERF_TIMER_H_
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
class PerfTimer
{
public:
PerfTimer();
~PerfTimer();
void reset();
void start();
void stop();
const double duration()const;
const double duration_milli_seconds()const;
const double duration_micro_seconds()const;
private:
#ifdef _WIN32
__int64 m_start_time;
__int64 m_stop_time;
__int64 m_freq;
#else
struct timeval m_start_time;
struct timeval m_stop_time;
#endif
};
#endif
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include "perftimer.h"
PerfTimer::PerfTimer()
{
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER *)&m_freq);
#endif
reset();
}
PerfTimer::~PerfTimer()
{
}
/// <summary>
/// resets the timer to ins initial state
/// </summary>
void PerfTimer::reset()
{
#ifdef _WIN32
m_start_time = 0;
m_stop_time = 0;
#endif
}
/// <summary>
/// sets the start time of the timer
/// </summary>
void PerfTimer::start()
{
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&m_start_time);
#else
struct timezone tz;
gettimeofday(&m_start_time, &tz);
#endif
}
/// <summary>
/// Sets the current end time for the timer. Repeated calls will reset the end time
/// of the timer.
/// </summary>
void PerfTimer::stop()
{
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&m_stop_time);
#else
struct timezone tz;
gettimeofday(&m_stop_time, &tz);
#endif
}
// Returns the duration of the timer (in seconds)
const double PerfTimer::duration()const
{
#ifdef _WIN32
return (double)(m_stop_time - m_start_time) / (double)m_freq;
#else
return (double)m_stop_time.tv_sec - (double)m_start_time.tv_sec +
(((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec)/1000000.0);
#endif
}
const double PerfTimer::duration_milli_seconds()const
{
#ifdef _WIN32
return ((double)(m_stop_time - m_start_time) / (double)m_freq) * 1000;
#else
return ((double)m_stop_time.tv_sec - (double)m_start_time.tv_sec) * 1000.0 +
(((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec)/1000.0);
#endif
}
const double PerfTimer::duration_micro_seconds()const
{
#ifdef _WIN32
return ((double)(m_stop_time - m_start_time) / (double)m_freq) * 1000 * 1000;
#else
return ((double)m_stop_time.tv_sec - (double)m_start_time.tv_sec) * 1000000.0 +
((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec);
#endif
}
PerfTimer::PerfTimer()
{
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER *)&m_freq);
#endif
reset();
}
PerfTimer::~PerfTimer()
{
}
/// <summary>
/// resets the timer to ins initial state
/// </summary>
void PerfTimer::reset()
{
#ifdef _WIN32
m_start_time = 0;
m_stop_time = 0;
#endif
}
/// <summary>
/// sets the start time of the timer
/// </summary>
void PerfTimer::start()
{
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&m_start_time);
#else
struct timezone tz;
gettimeofday(&m_start_time, &tz);
#endif
}
/// <summary>
/// Sets the current end time for the timer. Repeated calls will reset the end time
/// of the timer.
/// </summary>
void PerfTimer::stop()
{
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&m_stop_time);
#else
struct timezone tz;
gettimeofday(&m_stop_time, &tz);
#endif
}
// Returns the duration of the timer (in seconds)
const double PerfTimer::duration()const
{
#ifdef _WIN32
return (double)(m_stop_time - m_start_time) / (double)m_freq;
#else
return (double)m_stop_time.tv_sec - (double)m_start_time.tv_sec +
(((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec)/1000000.0);
#endif
}
const double PerfTimer::duration_milli_seconds()const
{
#ifdef _WIN32
return ((double)(m_stop_time - m_start_time) / (double)m_freq) * 1000;
#else
return ((double)m_stop_time.tv_sec - (double)m_start_time.tv_sec) * 1000.0 +
(((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec)/1000.0);
#endif
}
const double PerfTimer::duration_micro_seconds()const
{
#ifdef _WIN32
return ((double)(m_stop_time - m_start_time) / (double)m_freq) * 1000 * 1000;
#else
return ((double)m_stop_time.tv_sec - (double)m_start_time.tv_sec) * 1000000.0 +
((double)m_stop_time.tv_usec - (double)m_start_time.tv_usec);
#endif
}