青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

tommy

It's hard to tell the world we live in is either a reality or a dream
posts - 52, comments - 17, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

計時輔助類

Posted on 2006-04-01 11:10 Tommy Liang 閱讀(855) 評論(1)  編輯 收藏 引用 所屬分類: 讀書筆記《C++圖算法》

《windows圖形編程》有講:

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指令的運行時間

????KTimer()
????
{
????????m_overhead?
= ? 0 ;
????????Start();
????????m_overhead?
= ?Stop();
????}

????
void ?Start();
????unsigned?__int64?Stop();
????unsigned?unsigned?GetCPUSpeed();

}
;

KTimer.cpp
#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?
<<?"開始測試?cpu速度.."?<<?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;
}

Feedback

# re: 計時輔助類  回復  更多評論   

2006-09-25 17:44 by 子彈
//========================================================================
// CPUSPEED
//
// CPU Timer for the Action, Arcade, Strategy Games Group, a part of
// the Entertainment Business Unit at Microsoft.
//
// (c) Copyright 1999-2000 Microsoft Corporation.
// Written by Michael Lyons
//
// USED WITH PERMISSION
//
//========================================================================

//========================================================================
// Content References in Game Coding Complete 2nd Edition
//
// GetCPUSpeed - Chapter 5, page 135
//========================================================================


#include "GameCodeStd.h"

#define SLEEPTIME 0


//========================================================================
// define static variables
//========================================================================
static int s_milliseconds;
static __int64 s_ticks;

static int s_milliseconds0;
static __int64 s_ticks0;

//========================================================================
// fabs
//
// floating point absolute value function
//========================================================================
#if 0
#pragma message("Dsiabled local fabs()implementation to prevent collision w/impl in VS.NET 2k3")
float inline fabs(float a)
{
if (a < 0.0f)
return -a;
else
return a;
}
#endif
//========================================================================
// StartTimingCPU
//
// Call this function to start timing the CPU. It takes the CPU tick
// count and the current time and stores it. Then, while you do other
// things, and the OS task switches, the counters continue to count, and
// when you call UpdateCPUTime, the measured speed is accurate.
//
//========================================================================
int StartTimingCPU()
{
//
// detect ability to get info
//
__asm
{
pushfd ; push extended flags
pop eax ; store eflags into eax
mov ebx, eax ; save EBX for testing later
xor eax, (1<<21) ; switch bit 21
push eax ; push eflags
popfd ; pop them again
pushfd ; push extended flags
pop eax ; store eflags into eax
cmp eax, ebx ; see if bit 21 has changed
jz no_cpuid ; make sure it's now on
}

//
// make ourselves high priority just for the time between
// when we measure the time and the CPU ticks
//
DWORD dwPriorityClass = GetPriorityClass(GetCurrentProcess());
int dwThreadPriority = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

//
// start timing
//
s_milliseconds0 = (int)timeGetTime();

__asm
{
lea ecx, s_ticks0 ; get the offset
mov dword ptr [ecx], 0 ; zero the memory
mov dword ptr [ecx+4], 0 ;
rdtsc ; read time-stamp counter
mov [ecx], eax ; store the negative
mov [ecx+4], edx ; in the variable
}

//
// restore thread priority
//
SetThreadPriority(GetCurrentThread(), dwThreadPriority);
SetPriorityClass(GetCurrentProcess(), dwPriorityClass);

return 0;

no_cpuid:
return -1;
}

//========================================================================
// UpdateCPUTime
//
// This function stops timing the CPU by adjusting the timers to account
// for the amount of elapsed time and the number of CPU cycles taked
// during the timing period.
//========================================================================
void UpdateCPUTime()
{
//
// make ourselves high priority just for the time between
// when we measure the time and the CPU ticks
//
DWORD dwPriorityClass = GetPriorityClass(GetCurrentProcess());
int dwThreadPriority = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

//
// get the times
//
s_milliseconds = -s_milliseconds0;
s_ticks = -s_ticks0;

s_milliseconds += (int)timeGetTime();

__asm
{
lea ecx, s_ticks ; get the offset
rdtsc ; read time-stamp counter
add [ecx], eax ; add the tick count
adc [ecx+4], edx ;
}

//
// restore thread priority
//
SetThreadPriority(GetCurrentThread(), dwThreadPriority);
SetPriorityClass(GetCurrentProcess(), dwPriorityClass);

return;
}

//========================================================================
// CalcCPUSpeed
//
// This function takes the measured values and returns a speed that
// represents a common possible CPU speed.
//========================================================================
int CalcCPUSpeed()
{
//
// get the actual cpu speed in MHz, and
// then find the one in the CPU speed list
// that is closest
//
const struct tagCPUSPEEDS
{
float fSpeed;
int iSpeed;
} cpu_speeds[] =
{
//
// valid CPU speeds that are not integrally divisible by
// 16.67 MHz
//
{ 60.00f, 60 },
{ 75.00f, 75 },
{ 90.00f, 90 },
{ 120.00f, 120 },
{ 180.00f, 180 },
};

//
// find the closest one
//
float fSpeed=((float)s_ticks)/((float)s_milliseconds*1000.0f);
int iSpeed=cpu_speeds[0].iSpeed;
float fDiff=(float)fabs(fSpeed-cpu_speeds[0].fSpeed);

for (int i=1 ; i<sizeof(cpu_speeds)/sizeof(cpu_speeds[0]) ; i++)
{
float fTmpDiff = (float)fabs(fSpeed-cpu_speeds[i].fSpeed);

if (fTmpDiff < fDiff)
{
iSpeed=cpu_speeds[i].iSpeed;
fDiff=fTmpDiff;
}
}

//
// now, calculate the nearest multiple of fIncr
// speed
//

//
// now, if the closest one is not within one incr, calculate
// the nearest multiple of fIncr speed and see if that's
// closer
//
const float fIncr=16.66666666666666666666667f;
const int iIncr=4267; // fIncr << 8

//if (fDiff > fIncr)
{
//
// get the number of fIncr quantums the speed is
//
int iQuantums = (int)((fSpeed / fIncr) + 0.5f);
float fQuantumSpeed = (float)iQuantums * fIncr;
float fTmpDiff = (float)fabs(fQuantumSpeed - fSpeed);

if (fTmpDiff < fDiff)
{
iSpeed = (iQuantums * iIncr) >> 8;
fDiff=fTmpDiff;
}
}

return iSpeed;
}


//========================================================================
// GetCPUSpeed
//
// Gets the CPU speed by timing it for 1 second.
//========================================================================
int GetCPUSpeed()
{
static int CPU_SPEED = 0;

if(CPU_SPEED!=0)
{
//This will assure that the 0.5 second delay happens only once
return CPU_SPEED;
}

if (StartTimingCPU())
return 0;

//This will lock the application for 1 second
do
{
UpdateCPUTime();
Sleep(SLEEPTIME);
} while (s_milliseconds < 1000);

CPU_SPEED = CalcCPUSpeed();
return CPU_SPEED;
}


代碼是《Game Coding Complete》上的……


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产欧美日韩亚洲精品| 在线观看日韩精品| 国产精品欧美日韩一区| 亚洲第一偷拍| 欧美中文字幕| 亚洲一区日本| 欧美日韩dvd在线观看| 亚洲国产91色在线| 美腿丝袜亚洲色图| 久久精品免费观看| 国产日韩欧美一区二区三区四区 | 国产精品久久久久久久久久免费 | 最新亚洲一区| 美女脱光内衣内裤视频久久影院| 亚洲欧美一区二区精品久久久| 国产精品久久久久久久久久久久久 | 久久久水蜜桃| 国内偷自视频区视频综合| 久久精品亚洲国产奇米99| 午夜激情久久久| 国产欧美大片| 蜜桃伊人久久| 欧美国产一区二区在线观看| 99日韩精品| 9久草视频在线视频精品| 欧美欧美天天天天操| 一本一本a久久| 亚洲无吗在线| 国产综合香蕉五月婷在线| 麻豆成人综合网| 欧美国产精品日韩| 亚洲一二三区在线观看| 午夜伦欧美伦电影理论片| 一区精品在线播放| 亚洲国产婷婷| 欧美日韩一区二区三区在线视频| 一区二区三区视频观看| 亚洲一区二区三区免费视频| 国产一区二区三区在线观看视频 | 亚洲国产成人久久| 亚洲伦理一区| 国产精品久久福利| 麻豆91精品| 欧美日本亚洲韩国国产| 午夜精品福利一区二区三区av| 欧美在线啊v一区| 亚洲国产日韩一区| 亚洲视频二区| 在线精品福利| 亚洲调教视频在线观看| 亚洲高清三级视频| 亚洲午夜国产一区99re久久| 精品白丝av| 亚洲美女视频在线观看| 国内精品久久久久伊人av| 亚洲精品一级| 国产字幕视频一区二区| 日韩视频一区二区在线观看| 韩国一区二区三区美女美女秀| 亚洲精品欧美精品| 黄色精品一区| 亚洲伊人久久综合| 一区二区欧美在线| 久久在线免费观看| 久久精品国产欧美亚洲人人爽| 欧美区在线播放| 你懂的国产精品| 国内成+人亚洲+欧美+综合在线| 亚洲日本电影在线| 一色屋精品视频免费看| 亚洲一区免费观看| 一区二区三区高清不卡| 久久综合中文字幕| 久久久青草青青国产亚洲免观| 国产精品国产三级国产aⅴ浪潮| 亚洲欧洲日本在线| 亚洲国产精品va在线观看黑人| 亚洲一区尤物| 亚洲免费在线看| 欧美欧美天天天天操| 亚洲国产精品一区二区第四页av| 国产综合视频在线观看| 性久久久久久久久| 香蕉免费一区二区三区在线观看 | 久久久久成人精品免费播放动漫| 亚洲欧美一区二区三区极速播放| 免费日韩视频| 亚洲第一区在线| 亚洲韩日在线| 欧美激情1区2区| 欧美成人综合网站| 国内精品免费午夜毛片| 欧美一区国产一区| 久久久亚洲综合| 国产曰批免费观看久久久| 新狼窝色av性久久久久久| 久久久91精品国产一区二区精品| 国产精品一二一区| 亚洲欧美另类在线观看| 午夜精品久久| 久久se精品一区二区| 欧美69wwwcom| 欧美激情亚洲精品| 亚洲卡通欧美制服中文| 欧美日韩1区| 亚洲校园激情| 久久综合九色欧美综合狠狠| 亚洲电影第三页| 欧美精品在线观看91| 亚洲色在线视频| 久久久xxx| 亚洲高清网站| 欧美久久99| 午夜精品一区二区三区四区| 激情亚洲成人| 久热这里只精品99re8久| 亚洲国产小视频在线观看| 中文在线一区| 国产婷婷色一区二区三区| 久久久福利视频| 欧美寡妇偷汉性猛交| 国产精品99久久久久久有的能看 | 午夜久久99| 国产资源精品在线观看| 蜜桃av噜噜一区二区三区| 日韩午夜在线播放| 久久久www成人免费毛片麻豆| 亚洲激情视频网站| 国产精品久久久一区二区| 久久国产精品毛片| 亚洲日本无吗高清不卡| 欧美制服第一页| 亚洲国产日韩欧美| 国产欧美日韩视频一区二区| 欧美+日本+国产+在线a∨观看| 一区二区日韩伦理片| 久久夜色精品国产欧美乱极品| 亚洲精品女av网站| 国产午夜精品理论片a级探花| 免费看的黄色欧美网站| 亚洲综合999| 亚洲精品一区二区三区福利| 久久久欧美精品| 亚洲一区二区三区四区五区午夜| 极品日韩久久| 国产精品综合不卡av| 欧美日韩国产精品专区| 久久精品一区二区三区中文字幕 | 亚洲一区制服诱惑| 亚洲人人精品| 欧美大胆人体视频| 久久丁香综合五月国产三级网站| 99精品视频一区二区三区| 精品av久久久久电影| 国产精品亚洲成人| 欧美日韩精品伦理作品在线免费观看| 久久精品亚洲精品| 亚洲欧美日韩网| 一本久久综合亚洲鲁鲁五月天| 免费日韩av片| 久久久久久69| 久久国产精品72免费观看| 亚洲影院污污.| 99亚洲伊人久久精品影院红桃| 在线观看成人av| 国产日韩一区| 国产目拍亚洲精品99久久精品 | 久久成人羞羞网站| 午夜精品美女久久久久av福利| 99re66热这里只有精品4| 亚洲经典视频在线观看| 尤物精品在线| 亚洲高清在线视频| 亚洲激情视频网| 亚洲欧洲精品一区二区三区不卡 | 亚洲图片你懂的| 99日韩精品| 一区二区成人精品| 亚洲天堂免费观看| 亚洲欧美区自拍先锋| 亚洲欧美激情诱惑| 亚洲欧美中文字幕| 欧美在线视频在线播放完整版免费观看| 亚洲欧美日韩国产中文| 午夜久久影院| 久久精品国产99精品国产亚洲性色| 欧美在线观看日本一区| 久久久久久穴| 欧美成人小视频| 亚洲高清一二三区| 亚洲美女色禁图| 亚洲欧美国产另类| 欧美一区=区| 久久亚洲美女| 欧美美女操人视频| 国产精品mm| 国产一区清纯| 亚洲精品你懂的| 性欧美办公室18xxxxhd| 久久亚洲精品一区|