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

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 閱讀(851) 評論(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》上的……

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产亚洲欧美色| 亚洲国产免费看| 国产精品视频免费在线观看| 欧美看片网站| 麻豆精品网站| 欧美黄色网络| 国产精品wwwwww| 亚洲视频观看| 亚洲欧美高清| 欧美在线看片| 久久亚洲国产成人| 久久综合伊人| 亚洲第一免费播放区| 99re热精品| 欧美亚洲网站| 欧美二区视频| 国产精品三级久久久久久电影| 日韩视频免费观看高清在线视频| 亚洲手机成人高清视频| 亚洲欧美一级二级三级| 久久精品一区四区| 久久永久免费| 欧美日韩国产美女| 国产有码一区二区| 亚洲免费观看高清完整版在线观看熊| 99精品视频一区二区三区| 新67194成人永久网站| 久久综合久久88| 99热在线精品观看| 久久艳片www.17c.com| 欧美午夜电影完整版| 尤物精品在线| 欧美伊人影院| 亚洲精品国精品久久99热| 午夜视频在线观看一区二区三区| 欧美成人有码| 一区二区在线不卡| 欧美一区二区久久久| 亚洲激情六月丁香| 久久久国产精彩视频美女艺术照福利| 欧美日韩小视频| 亚洲二区视频在线| 久久精品系列| 亚洲一区在线观看视频| 欧美成人性生活| 在线看片成人| 久久久久免费| 午夜久久一区| 国产麻豆91精品| 亚洲女同同性videoxma| 亚洲国内精品| 免费高清在线视频一区·| 在线精品视频一区二区| 99亚洲一区二区| 在线亚洲伦理| 欧美激情中文不卡| 欧美自拍偷拍午夜视频| 国产精品久久久久久久久久三级 | 亚洲一区二区三区影院| 欧美成在线视频| 亚洲国产精品va在线看黑人动漫 | 亚洲理伦在线| 欧美国产视频一区二区| 亚洲国产日韩欧美在线图片 | 亚洲午夜一区二区| 亚洲精品小视频在线观看| 欧美高清不卡在线| 亚洲精品视频一区| 欧美激情2020午夜免费观看| 久久久久高清| 尤物yw午夜国产精品视频明星| 免费观看亚洲视频大全| 久久精品人人做人人爽电影蜜月| 国产一区二区精品久久99| 久久久www成人免费毛片麻豆| 欧美一区激情视频在线观看| 韩国女主播一区| 免费成人在线视频网站| 欧美电影打屁股sp| 亚洲视频一区二区免费在线观看| 亚洲美女色禁图| 国产精品裸体一区二区三区| 久久成人免费网| 久久精品亚洲一区二区三区浴池| 在线观看视频一区二区| 亚洲欧洲一区二区三区在线观看| 欧美精品在线视频观看| 亚洲专区一区| 欧美中日韩免费视频| 亚洲激情专区| 亚洲视频在线观看视频| 韩国一区二区在线观看| 亚洲日本中文| 国产日韩精品在线观看| 免费观看国产成人| 欧美日韩色一区| 久久久精品免费视频| 欧美jizz19hd性欧美| 午夜精品福利一区二区蜜股av| 欧美在线|欧美| 一区二区三区精品久久久| 午夜精品影院| 在线视频一区观看| 久久精品综合| 午夜宅男欧美| 欧美精品www| 老司机午夜精品视频| 欧美日韩一级视频| 久久综合中文字幕| 一级成人国产| 91久久久久久| 国产欧美一区二区精品忘忧草| 欧美gay视频激情| 国产精品毛片a∨一区二区三区|国 | 欧美有码视频| 欧美乱人伦中文字幕在线| 久久精品30| 欧美日韩在线视频一区二区| 欧美成人综合网站| 国产拍揄自揄精品视频麻豆| 日韩图片一区| 亚洲精品一区在线观看香蕉| 久久精品国产v日韩v亚洲 | 久久精品论坛| 欧美在线观看网址综合| 欧美日韩在线一区| 亚洲第一福利视频| 韩国一区二区三区在线观看| 亚洲欧美在线播放| 亚洲影院免费| 欧美日韩在线一区| 亚洲精品男同| 亚洲国产婷婷| 女生裸体视频一区二区三区| 欧美成人免费视频| 狠狠色丁香久久婷婷综合丁香| 性做久久久久久免费观看欧美 | 激情欧美丁香| 午夜精品久久久久久久99水蜜桃| 亚洲一区精品电影| 欧美精品99| 亚洲精品美女在线观看播放| 亚洲免费观看高清完整版在线观看| 老鸭窝毛片一区二区三区| 麻豆91精品91久久久的内涵| 国产在线乱码一区二区三区| 性欧美18~19sex高清播放| 久久高清一区| 精品成人国产| 浪潮色综合久久天堂| 欧美jizz19性欧美| 91久久香蕉国产日韩欧美9色| 欧美成人高清| 亚洲最新视频在线| 欧美一区亚洲二区| 国产亚洲人成网站在线观看| 欧美一区二区黄| 麻豆国产精品一区二区三区| 亚洲欧洲另类国产综合| 欧美日韩国产影院| 亚洲综合国产| 欧美成人一区二免费视频软件| 亚洲精品日本| 欧美午夜电影完整版| 欧美一区二区黄色| 亚洲高清在线观看| 亚洲精品视频免费在线观看| 国产精品国码视频| 99re6这里只有精品| 国产美女扒开尿口久久久| 性欧美在线看片a免费观看| 久久久国产精彩视频美女艺术照福利| 国产精品久久午夜夜伦鲁鲁| 香蕉免费一区二区三区在线观看| 欧美激情一区二区三区蜜桃视频 | 亚洲激情婷婷| 欧美伊人久久久久久久久影院| 尹人成人综合网| 欧美视频一区二区三区| 欧美在线免费视屏| 亚洲精品极品| 久久精品一区二区三区中文字幕| 亚洲人成网在线播放| 国产精品一区二区在线观看不卡| 麻豆精品在线观看| 亚洲深爱激情| 亚洲成色www8888| 欧美一区永久视频免费观看| 亚洲精品专区| 国产午夜亚洲精品不卡| 欧美日韩精品一区视频| 久久精品国产久精国产一老狼| 99精品欧美一区二区三区| 免费日韩成人| 久久精品国产一区二区三区免费看| 一本一本久久| 亚洲欧洲三级| 悠悠资源网亚洲青| 国产午夜精品在线观看| 国产精品成人播放|