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

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>
            午夜一区二区三区不卡视频| 久久蜜臀精品av| 亚洲激情av| 亚洲第一综合天堂另类专| 国内精品久久久久影院色| 国产一区观看| 怡红院精品视频| 91久久精品一区二区别| 91久久午夜| 99国产精品自拍| 羞羞漫画18久久大片| 久久久精品tv| 欧美激情一区二区三区蜜桃视频| 亚洲精品一二三区| 午夜国产不卡在线观看视频| 久久精品一区二区三区不卡| 欧美v日韩v国产v| 国产精品videosex极品| 黑人一区二区| 亚洲一区二区在线观看视频| 久久久久久亚洲精品杨幂换脸| 亚洲第一区在线| 亚洲日韩第九十九页| 欧美一区二视频| 欧美日韩国产bt| 尤物视频一区二区| 亚洲欧美视频在线| 亚洲国产精品激情在线观看 | 欧美激情二区三区| 国产精品99久久久久久久女警 | 国产精品毛片大码女人| 极品日韩久久| 艳妇臀荡乳欲伦亚洲一区| 久久久精品性| 亚洲无线视频| 欧美极品影院| 亚洲大片av| 久久精品国产99精品国产亚洲性色 | 免费成人小视频| 国产日韩精品在线观看| 日韩亚洲精品视频| 欧美大片一区二区三区| 小黄鸭精品密入口导航| 国产精品爱啪在线线免费观看| 怡红院av一区二区三区| 看欧美日韩国产| 国产午夜精品一区二区三区欧美 | 亚洲午夜91| 欧美日韩精品伦理作品在线免费观看| 伊人精品在线| 久久久久久久性| 亚洲欧美日韩精品久久奇米色影视| 欧美人与性动交α欧美精品济南到| 亚洲成人在线网站| 老鸭窝毛片一区二区三区| 午夜精品婷婷| 国产亚洲一区二区在线观看 | 野花国产精品入口| 亚洲精品你懂的| 久久久久综合一区二区三区| 国产一区二区精品久久99| 久久精品视频免费观看| 亚洲欧美资源在线| 国产视频在线观看一区二区三区 | 夜夜嗨av色综合久久久综合网| 亚洲国产精品va| 久久精品国产久精国产一老狼| 国产精品嫩草影院av蜜臀| 亚洲国产乱码最新视频| 蜜臀av性久久久久蜜臀aⅴ| 欧美制服丝袜| 伊人成人在线视频| 欧美α欧美αv大片| 欧美成人一区二区| 亚洲最黄网站| 午夜精品久久久久久久| 在线观看亚洲视频| 亚洲国产精品v| 国产精品99免费看 | 欧美制服丝袜| 国产欧美一二三区| 久久成人精品电影| 久久se精品一区精品二区| 国产一区二区三区丝袜| 久久久久久婷| 国产精品国产自产拍高清av王其| 91久久在线播放| 亚洲全部视频| 欧美性淫爽ww久久久久无| 老司机午夜免费精品视频| 亚洲欧洲日韩在线| 欧美激情成人在线| 欧美体内she精视频| 久久久久久久综合| 欧美大尺度在线| 亚洲欧美日本另类| 久久久噜噜噜久久| 亚洲免费视频观看| 久久人91精品久久久久久不卡| 艳女tv在线观看国产一区| 午夜日韩电影| av不卡在线| 麻豆成人在线| 久久久精品性| 国产精品素人视频| 亚洲欧洲在线免费| 黄色成人在线免费| 亚洲淫片在线视频| 日韩视频―中文字幕| 久久精品亚洲热| 性久久久久久| 欧美日韩中文字幕| 亚洲国产高清在线| 伊人一区二区三区久久精品| 亚洲女爱视频在线| 日韩视频永久免费观看| 欧美在线亚洲| 欧美一区二区三区精品电影| 欧美极品aⅴ影院| 欧美成人精品影院| 国产亚洲人成a一在线v站 | 亚洲一区二区高清| 99视频精品| 久久综合一区二区| 久久久久看片| 国产乱理伦片在线观看夜一区| 亚洲国产精品一区二区www在线| 欧美电影免费| 韩国美女久久| 午夜免费在线观看精品视频| 亚洲无毛电影| 欧美日韩亚洲高清一区二区| 亚洲福利久久| 欧美 日韩 国产精品免费观看| 老牛嫩草一区二区三区日本 | 亚洲福利国产| 亚洲精品黄色| 免费久久99精品国产| 欧美91精品| 日韩一二在线观看| 欧美视频成人| 亚洲视频欧洲视频| 午夜亚洲福利在线老司机| 国产伦理精品不卡| 久久精品99国产精品日本| 久久在线免费观看视频| 有码中文亚洲精品| 欧美成人综合| 亚洲国产91| 欧美大片国产精品| 欧美黑人国产人伦爽爽爽| 樱花yy私人影院亚洲| 久久久噜噜噜久久中文字幕色伊伊 | 亚洲欧美精品| 欧美在线视频全部完| 国产欧美精品一区二区三区介绍| 亚洲一区二区三区精品在线观看 | 亚洲国产精品一区二区尤物区| 怡红院精品视频在线观看极品| 老司机精品久久| 亚洲日本黄色| 亚洲欧美日韩一区| 国产一区三区三区| 免费观看国产成人| 在线视频亚洲| 久久久久久久综合色一本| 亚洲理论在线观看| 国产精品美女在线观看| 久久躁日日躁aaaaxxxx| 一区二区精品在线观看| 快播亚洲色图| 亚洲自拍电影| 亚洲二区在线观看| 国产精品久久久久av免费| 久久久精品五月天| 在线亚洲欧美视频| 狼人社综合社区| 亚洲欧美精品在线观看| 亚洲电影免费在线| 国产精品自拍网站| 欧美精品v国产精品v日韩精品 | 久久se精品一区二区| 亚洲国产欧美在线人成| 亚洲欧美日韩国产一区| 亚洲电影免费在线| 国产欧美一区二区三区另类精品| 欧美国产三级| 欧美亚洲免费在线| 亚洲精品乱码久久久久久黑人| 久久久久久一区| 亚洲欧美精品一区| av成人免费| 亚洲人成人一区二区在线观看| 国产精品美女午夜av| 欧美精品一区二区久久婷婷| 欧美中文字幕在线| 亚洲一区在线免费观看| 亚洲精品视频免费| 欧美成人影音| 久久先锋影音av|