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

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>
            在线电影国产精品| 另类av一区二区| 国产精品视频福利| 欧美日韩视频在线| 欧美理论在线播放| 欧美日韩一区二区三区免费看| 欧美精品七区| 国产精品午夜电影| 激情久久五月天| 亚洲精品一区二区三区福利| 日韩小视频在线观看| 正在播放亚洲| 久久精品99国产精品酒店日本| 久久嫩草精品久久久精品一 | 亚洲欧美另类中文字幕| 性做久久久久久久免费看| 久久精品色图| 欧美激情国产高清| 亚洲午夜电影在线观看| 久久久噜久噜久久综合| 欧美精品二区| 国产女主播一区| 99亚洲一区二区| 久久男人资源视频| 一区二区毛片| 女同性一区二区三区人了人一| 欧美日韩国产色综合一二三四| 国产麻豆日韩| 亚洲天堂av高清| 免费久久精品视频| 一区二区三区日韩欧美精品| 久久久高清一区二区三区| 欧美三级电影一区| 最新成人av网站| 久久夜色精品一区| 亚洲欧美亚洲| 国产精品高潮呻吟久久| 91久久在线播放| 美国三级日本三级久久99| 亚洲视频网在线直播| 欧美精品情趣视频| 久久午夜羞羞影院免费观看| 欧美视频福利| 亚洲区免费影片| 久久一区中文字幕| 一本色道久久综合亚洲二区三区| 久久嫩草精品久久久久| 国产日韩在线播放| 欧美bbbxxxxx| 亚洲国产精品一区| 久久蜜桃精品| 久久久精品国产免大香伊| 国产亚洲一级| 久久久久久一区二区| 性视频1819p久久| 国产欧美日韩综合一区在线观看| 亚洲视频二区| 正在播放欧美一区| 欧美日本国产一区| 一区二区三区黄色| 99综合在线| 国产精品美女| 欧美在线视频免费观看| 亚洲网站在线| 国产伦精品一区二区三区高清| 亚洲综合色丁香婷婷六月图片| 亚洲理论在线| 国产精品videosex极品| 亚洲欧美日韩国产综合精品二区| 日韩天堂在线视频| 国产精品国产a级| 欧美在线一区二区三区| 久久精品国产精品亚洲| 亚洲激情国产| 亚洲欧洲在线视频| 欧美日韩一卡二卡| 欧美伊人久久久久久午夜久久久久| 亚洲欧美综合网| 亚洲黄色av| 在线一区视频| 黄色亚洲精品| 亚洲成色777777在线观看影院| 免费短视频成人日韩| 一区二区国产精品| 亚洲女同在线| 91久久精品美女高潮| 亚洲欧洲日韩综合二区| 国产精品久久一区二区三区| 久久久久久电影| 欧美激情一二三区| 欧美一级视频一区二区| 久久影音先锋| 亚洲欧美日韩精品久久奇米色影视 | 欧美+亚洲+精品+三区| 亚洲精品中文字幕女同| 亚洲一区二区三区精品视频| 国产精品亚洲а∨天堂免在线| 欧美中文字幕精品| 国产亚洲精品v| 亚洲伊人伊色伊影伊综合网| 亚洲巨乳在线| 好吊妞**欧美| 日韩亚洲视频| 今天的高清视频免费播放成人| 亚洲激精日韩激精欧美精品| 国产精品爽爽ⅴa在线观看| 美女黄色成人网| 国产精品乱看| 日韩网站免费观看| 亚洲第一级黄色片| 亚洲欧美日韩国产中文| 99综合在线| 免费观看不卡av| 久久久精品一区| 国产精品美女主播| 亚洲精品社区| 亚洲三级免费电影| 久久免费视频观看| 久久久久久亚洲精品不卡4k岛国| 欧美体内she精视频| 亚洲高清一区二区三区| 精东粉嫩av免费一区二区三区| 亚洲一区国产一区| 亚洲一区二区欧美日韩| 欧美日韩成人一区二区| 亚洲国产天堂久久综合| 亚洲国产欧美国产综合一区| 久久九九99视频| 久久久久久久91| 国产在线播放一区二区三区| 亚洲免费一级电影| 欧美一区二区三区在线观看| 国产精品久久久91| av成人手机在线| 亚洲视频福利| 欧美日韩在线综合| 亚洲精品一二三| 这里只有视频精品| 欧美色区777第一页| 亚洲精品一区二区三区婷婷月| 91久久国产综合久久蜜月精品| 久久精品一二三| 蜜桃视频一区| 亚洲人成人一区二区在线观看 | 欧美日韩在线另类| 亚洲精品影院在线观看| 亚洲特级毛片| 国产农村妇女精品| 欧美在线免费观看视频| 亚洲精品一区二区在线观看| 99天天综合性| 最新日韩中文字幕| 亚洲欧美日韩精品综合在线观看| 麻豆av一区二区三区| 免费h精品视频在线播放| 亚洲第一色中文字幕| 麻豆亚洲精品| 91久久午夜| 先锋a资源在线看亚洲| 国产欧美日韩在线观看| 久久久久久久一区二区| 欧美激情一区二区三区在线视频观看 | 午夜视频在线观看一区| 久久超碰97人人做人人爱| 国产一区91| 欧美a级一区| 亚洲自拍偷拍色片视频| 久久综合狠狠综合久久综青草 | 国产一二精品视频| 亚洲欧美日韩精品久久久| 欧美一区二区视频在线观看| 精品999久久久| 欧美日韩精品久久久| 欧美在线综合| 最新热久久免费视频| 性欧美xxxx视频在线观看| 在线电影院国产精品| 国产精品v亚洲精品v日韩精品| 欧美专区日韩专区| 亚洲乱码一区二区| 久久婷婷综合激情| 在线综合亚洲| 最新日韩在线视频| 国产亚洲精品久久久| 欧美日韩一区二区欧美激情| 欧美中文在线字幕| 一本久久a久久精品亚洲| 久久综合九色欧美综合狠狠| 亚洲自拍16p| 亚洲精品一区二区三区在线观看| 国产乱码精品一区二区三区不卡| 男女精品网站| 久久精品夜色噜噜亚洲aⅴ| 一区二区欧美精品| 亚洲激情一区二区三区| 欧美成人久久| 久久亚洲精品一区| 久久精品99国产精品日本 | 亚洲国产精品va在线看黑人| 久久精彩视频|