open-source-search-engine/Timer.h

76 lines
1.7 KiB
C
Raw Normal View History

2013-08-03 00:12:24 +04:00
#ifndef _TIMER_H_
#define _TIMER_H_
class Timer {
public:
Timer () : m_start(0), m_end(0) {};
2014-10-30 22:36:39 +03:00
Timer ( int64_t startTime ) : m_start(startTime), m_end(0) {};
2013-08-03 00:12:24 +04:00
virtual ~Timer() {};
virtual void start () { m_start = gettimeofdayInMillisecondsLocal(); };
virtual void stop () { m_end = gettimeofdayInMillisecondsLocal(); };
2014-10-30 22:36:39 +03:00
int64_t getSpan () { return m_end-m_start; };
2013-08-03 00:12:24 +04:00
2014-10-30 22:36:39 +03:00
int64_t m_start;
int64_t m_end;
2013-08-03 00:12:24 +04:00
};
class AutoTimer : public Timer {
public:
AutoTimer ( char *subtype, char *name, char *klass = NULL )
: Timer(), m_subtype(subtype), m_name(name), m_class(klass) {
start(); };
~AutoTimer () {
stop();
if ( m_class )
log( LOG_TIMING, "%s: Took %lld ms for %s::%s",
m_subtype, getSpan(), m_class, m_name );
else
log( LOG_TIMING, "%s: Took %lld ms for %s",
m_subtype, getSpan(), m_name );
};
char *m_subtype;
char *m_name;
char *m_class;
};
class MicroTimer {
public:
MicroTimer () : m_start(0), m_end(0) {};
virtual ~MicroTimer () {};
virtual void start () { m_start = gettimeofdayInMicroseconds(); };
virtual void stop () { m_end = gettimeofdayInMicroseconds(); };
uint64_t getSpan () { return m_end-m_start; };
uint64_t m_start;
uint64_t m_end;
};
class AutoMicroTimer : public MicroTimer {
public:
AutoMicroTimer ( char *subtype, char *name, char *klass = NULL )
: MicroTimer(), m_subtype(subtype), m_name(name), m_class(klass) {
start(); };
~AutoMicroTimer () {
stop();
if ( m_class )
2014-11-11 01:45:11 +03:00
log( LOG_TIMING, "%s: Took %"UINT64" microseconds for %s::%s",
2013-08-03 00:12:24 +04:00
m_subtype, getSpan(), m_class, m_name );
else
2014-11-11 01:45:11 +03:00
log( LOG_TIMING, "%s: Took %"UINT64" microseconds for %s",
2013-08-03 00:12:24 +04:00
m_subtype, getSpan(), m_name );
};
char *m_subtype;
char *m_name;
char *m_class;
};
#endif // _TIMER_H_