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

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>
            亚洲国产日韩欧美综合久久| 欧美在线综合| 亚洲欧美激情一区| 一本到高清视频免费精品| 亚洲人成人77777线观看| 欧美在线黄色| 欧美在线一二三四区| 欧美专区在线观看| 久久久久国产精品厨房| 久久综合九色| 欧美人与禽性xxxxx杂性| 欧美日韩一区二区三区在线观看免| 欧美精品色一区二区三区| 国产精品高清一区二区三区| 国产精品久久久久久久久久久久久 | 性欧美xxxx大乳国产app| 羞羞答答国产精品www一本| 久久精品免费看| 久热精品视频在线观看| 欧美精品午夜| 欧美亚韩一区| 一区二区亚洲| 在线综合欧美| 欧美在线网站| 亚洲影音先锋| 久久免费黄色| 亚洲人成人99网站| 亚洲天堂视频在线观看| 久久久久久久久久码影片| 欧美日本国产一区| 悠悠资源网亚洲青| 亚洲一区二区精品在线观看| 免费成人在线视频网站| 中文国产一区| 欧美丰满高潮xxxx喷水动漫| 国产欧亚日韩视频| 在线亚洲观看| 亚洲电影在线| 久久三级福利| 国产在线精品二区| 性做久久久久久久久| 亚洲国产经典视频| 久久久.com| 国产一区二区久久久| 亚洲免费视频成人| 亚洲七七久久综合桃花剧情介绍| 欧美亚洲一级片| 国产精品成人一区二区三区夜夜夜 | 嫩草成人www欧美| 亚洲一区综合| 欧美日韩福利| 亚洲精品日韩久久| 欧美福利视频| 久久婷婷av| 极品尤物久久久av免费看| 午夜精品久久久久久久99热浪潮| 亚洲人午夜精品| 欧美福利电影在线观看| 亚洲国产一二三| 欧美成年网站| 久久午夜精品一区二区| 国外成人在线| 久久久人成影片一区二区三区| 亚洲尤物在线视频观看| 国产精品萝li| 欧美在线视频免费| 香蕉免费一区二区三区在线观看| 欧美一区三区二区在线观看| 欧美午夜在线一二页| 亚洲一区精彩视频| 亚洲视频一区| 国产精品久久久久久久久久三级| 亚洲视频碰碰| 亚洲日本黄色| 欧美日韩精品免费| 亚洲在线网站| 亚洲欧美乱综合| 国产一区二区三区不卡在线观看| 久久久欧美精品sm网站| 久久久久久久综合日本| 亚洲国产精品久久91精品| 亚洲国产精品va在看黑人| 欧美国产亚洲精品久久久8v| 9人人澡人人爽人人精品| 亚洲最新中文字幕| 国产情人综合久久777777| 久久综合激情| 欧美成人中文| 欧美一级欧美一级在线播放| 久久精品国产欧美激情| 亚洲美女毛片| 宅男精品视频| 一区视频在线| 亚洲精品少妇30p| 国产欧美日韩在线| 欧美大色视频| 国产精品日韩专区| 亚洲福利小视频| 亚洲激情在线视频| 国产精品国产三级国产专区53 | 午夜精品视频在线观看| 影音先锋成人资源站| 亚洲另类黄色| 狠狠色丁香婷婷综合影院| 亚洲破处大片| 国产专区综合网| 亚洲七七久久综合桃花剧情介绍| 国产欧美一区二区三区在线老狼| 亚洲电影免费观看高清完整版| 欧美日韩一区二区三区在线 | 妖精视频成人观看www| 午夜欧美精品久久久久久久| 亚洲欧洲一区二区天堂久久| 亚洲一区在线播放| 日韩午夜激情| 久久av一区二区| 亚洲综合导航| 欧美精品亚洲一区二区在线播放| 欧美影院视频| 欧美高清hd18日本| 另类春色校园亚洲| 国产精品区免费视频| 亚洲激情社区| 永久免费毛片在线播放不卡| 亚洲欧美制服另类日韩| 亚洲精品视频一区| 久久精品噜噜噜成人av农村| 午夜精品在线看| 欧美日韩伦理在线免费| 欧美激情一区二区三区成人 | 亚洲女人av| 欧美电影免费观看高清| 免费一级欧美片在线播放| 国产精品视频福利| 一本大道久久a久久精二百| 亚洲精品在线免费| 免费在线成人| 欧美激情第一页xxx| 黄色日韩在线| 欧美一区二视频在线免费观看| 亚洲欧美中文字幕| 国产精品hd| 亚洲人永久免费| 亚洲毛片视频| 欧美日本精品一区二区三区| 亚洲精品国产精品国自产在线| 亚洲国产三级| 欧美成人日本| 欧美激情一区二区| 亚洲精选在线观看| 欧美三级中文字幕在线观看| 日韩午夜精品| 亚洲综合色丁香婷婷六月图片| 欧美欧美天天天天操| 一区二区三区高清| 亚洲欧美日韩在线播放| 国产美女一区| 久久人人爽人人爽| 亚洲国产91精品在线观看| 日韩网站在线看片你懂的| 欧美区视频在线观看| 一本综合精品| 久久久久久久性| 91久久精品视频| 欧美日韩精品二区第二页| 亚洲深夜影院| 久久久青草婷婷精品综合日韩| 亚洲青色在线| 欧美视频在线观看免费网址| 午夜精品久久| 牛牛影视久久网| 亚洲一区欧美二区| 在线成人亚洲| 欧美日韩在线精品| 亚洲一区二区三区在线| 久久久91精品国产一区二区精品| 韩国成人精品a∨在线观看| 免费在线亚洲| 亚洲欧美日韩综合国产aⅴ| 欧美激情第10页| 亚洲欧美日韩一区二区三区在线观看 | 亚洲欧美成人一区二区三区| 久久精品国产精品亚洲| 亚洲精品一区二区三区四区高清| 国产精品久久毛片a| 久久久国产亚洲精品| 亚洲免费av网站| 噜噜噜91成人网| 宅男在线国产精品| 亚洲国产合集| 国产午夜精品久久| 欧美久久久久久蜜桃| 欧美亚洲在线视频| 日韩写真视频在线观看| 麻豆成人精品| 亚洲欧美日韩国产综合精品二区| 亚洲欧洲午夜| 一区二区在线观看av| 国产日本亚洲高清| 欧美日韩一区二区在线观看视频|