rdtsct tricky hack

static inline uint64 
rdtsc() 
{
    unsigned int lo, hi;
#ifdef __linux__
    /* We cannot use "=A", since this would use %rax on x86_64 */
    __asm__ __volatile__ (
        "rdtsc" 
        : "=a" (lo), "=d" (hi)
    );
#else
#error "not done yet "
#endif
    return (uint64)hi << 32 | lo;
}

uint64 GetCpuFreq()
{
    static uint64 freq = 0;
    char buf[1024], *p[2];
    
    if (0 != freq) {
        return freq;
    }
    
    FILE* fp = fopen("/proc/cpuinfo", "rb");
    if (fp != NULL) {
        while (fgets(buf, sizeof(buf), fp) != NULL) {
            if (2==SplitString(buf, ':', p, 2) && 0==strnicmp(p[0], "cpu MHz", 7)) {
                double f = strtod(p[1], NULL);
                freq = (uint64)(f * 1000000.0);
                //printf("freq=%u\n", freq);
                break;
            }
        }
        fclose(fp);
    }
    if (0 == freq) {
        freq = CalcCpuFreq();
    }
    return freq;
}
static uint64 
CalcCpuFreq()
{
    uint64 t1;
    uint64 t2;
    
    t1 = rdtsc();
    Sleep(100);
    t2 = rdtsc();
    return (t2-t1)*10;
}

class CCycleTimer
{
public:
	CCycleTimer(bool bStart = false) { Init(bStart); }
	~CCycleTimer() {}

	void Start(bool bReset = false) {
		uint64 i = rdtsc();
		
		Lock();
		if (bReset)
			m_Elapsed = 0;
		m_Start = i;
		UnLock();
	}
	void Stop() {
		uint64 i = rdtsc();
		
		Lock();
		if (0 != m_Start) {
			m_Elapsed += i - m_Start;
			m_Start = 0;
		}
		UnLock();
	}

	uint64 Peek() {
		uint64 n;

		Lock();
		n = m_Elapsed;
		if (0 != m_Start) {
			n += rdtsc() - m_Start;
		}
		UnLock();

		return n;
	}

	bool IsRunning() {
		bool b;
		
		Lock();
		b = m_Start != 0;
		UnLock();
		return b;
	}

	const double Elapsed() {		// Returns elapsed time in seconds
		return (double)Peek() / m_Freq;
	}
	const double Elapsedms() {		// Returns elapsed time in milliseconds 
		return ((double)Peek() / m_Freq) * 1000.0;
	}
	const double Elapsedus() {		// Returns elapsed time in microseconds
		return ((double)Peek() / m_Freq) * 1000000.0;
	}
	
	void Init(bool bStart) {
		m_Elapsed = 0; 
		m_Start = 0;
		m_Freq = GetCpuFreq();
        if (bStart) {
			Start();
        }
	}

protected:
	void Lock() {}
	void UnLock() {}

private:
	uint64 m_Start, m_Freq, m_Elapsed;
};

class CLocalTimer
{
    CCycleTimer& m_timer;
public:
    CLocalTimer(CCycleTimer& t) : m_timer(t) { m_timer.Start(); }
    ~CLocalTimer() { m_timer.Stop(); }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值