Consider the code from SO: Get CPU cycle count?
static inline uint64_t get_cycles()
{
uint64_t t;
__asm volatile ("rdtsc" : "=A"(t));
return t;
}
and implement a class like the following:
class ScopedTimer
{
public:
ScopedTime ()
{
m_start = get_cycles ()
}
~ScopedTimer ()
{
auto diff = get_cycles() - m_start;
std::cout << "Takes " << diff << " cycles" << std::endl;
}
private:
uint64_t m_start;
};
Finally you can simply use that class in your code with:
void job () {
ScopedTimer timer;
// do some job
// leaving the scope will automatically print the message in the desctrutor.
}
I have some similar code that automatically counts some statistics in different categories.
However, mainly in the destructor you have to accumulate the cycles into a statistic class or something else.