锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
KTimer.h
#pragma聽once
inline聽unsigned聽__int64聽GetCycleCount(
void
)
{
聽聽聽聽_asm聽_emit聽
0x0F
聽聽聽聽_asm聽_emit聽
0x31
}
class
聽KTimer聽聽
{
聽聽聽聽unsigned聽__int64聽m_startcycle;
public
:
聽聽聽聽unsigned聽__int64聽m_overhead;聽聽聽聽
//
RTSC鎸囦護鐨勮繍琛屾椂闂?/span>
聽聽聽聽KTimer()
聽聽聽聽
{
聽聽聽聽聽聽聽聽m_overhead聽
=
聽
0
;
聽聽聽聽聽聽聽聽Start();
聽聽聽聽聽聽聽聽m_overhead聽
=
聽Stop();
聽聽聽聽}
聽聽聽聽
void
聽Start();
聽聽聽聽unsigned聽__int64聽Stop();
聽聽聽聽unsigned聽unsigned聽GetCPUSpeed();
}
;
#include聽"KTimer.h"
#include聽<iostream>
#include聽<windows.h>
void聽KTimer::Start()

{
聽聽聽聽m_startcycle聽=聽GetCycleCount();
}
unsigned聽__int64聽KTimer::Stop()

{
聽聽聽聽return聽GetCycleCount()聽-聽m_startcycle聽-聽m_overhead;
}
unsigned聽unsigned聽KTimer::GetCPUSpeed()

{
聽聽聽聽cout聽<<聽"寮濮嬫祴璇暵燾pu閫熷害.."聽<<聽endl;
聽聽聽聽Start();
聽聽聽聽Sleep(1000);
聽聽聽聽unsigned聽cputime聽=聽Stop();
聽聽聽聽unsigned聽cpuspeed10聽=聽(unsigned)(cputime/100000);
聽聽聽聽cout聽<<聽"CPU閫熷害聽姣忕:"聽<<聽cputime聽<<聽"聽clocks"聽<<聽endl;
聽聽聽聽return聽cpuspeed10聽==聽0聽?聽1聽:聽cpuspeed10;
}
#include聽"stdafx.h"
#include聽<tchar.h>
#include聽<windows.h>
#include聽<iostream>
#include聽"KTimer.h"
int聽main(int聽argc,聽char*聽argv[])

{聽聽聽聽
聽聽聽聽KTimer聽timer;
聽聽聽聽unsigned聽cpuspeed10聽=聽timer.GetCPUSpeed();
聽聽聽聽timer.Start();
聽聽聽聽
//
鍋氳楁椂鎿嶄綔
聽聽聽聽
聽聽聽聽unsigned聽time聽=聽timer.Stop();
聽聽聽聽TCHAR聽mess[128];
聽聽聽聽wsprintf(mess,_T("鑰楁椂:%d聽ns"),聽time聽*聽10000聽/聽cpuspeed10);
聽聽聽聽cout聽<<聽mess聽<<聽endl;
聽聽聽聽return聽0;
}