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

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>
            欧美激情免费观看| 久久久久久久91| 久久国产色av| 亚洲国产精品美女| 亚洲电影av| 蜜臀99久久精品久久久久久软件 | 翔田千里一区二区| 韩国av一区二区三区在线观看| 欧美一区二区三区在线观看视频 | 久久一综合视频| 亚洲免费成人av电影| 亚洲作爱视频| 亚洲国产精品一区在线观看不卡| 欧美激情国产精品| 国产日韩欧美精品| 99re热这里只有精品视频| 国产区精品视频| 久久成年人视频| 日韩午夜在线观看视频| 国产精品夜夜夜| 午夜精品一区二区三区四区 | 欧美一区视频| 亚洲第一视频网站| 亚洲午夜在线视频| 在线欧美福利| 亚洲午夜三级在线| 亚洲伊人网站| 亚洲高清一区二| 欧美在线亚洲在线| 国产精品99久久99久久久二8| 亚洲小说欧美另类婷婷| 国产精品色婷婷| 亚洲激情电影中文字幕| 韩国在线视频一区| 一级日韩一区在线观看| 亚洲国产91色在线| 午夜精品亚洲一区二区三区嫩草| 欧美午夜精品电影| 亚洲国产老妈| 亚洲视频免费在线| 国产亚洲一区在线播放| 久久精品亚洲一区| 亚洲国产精品久久久久| 在线亚洲免费| 国产视频一区二区在线观看 | 激情成人av在线| 美女主播一区| 亚洲欧美电影在线观看| 久久午夜精品一区二区| 国产一区二区丝袜高跟鞋图片| 久久久www成人免费毛片麻豆| 欧美成人午夜影院| 亚洲欧美日韩精品久久亚洲区| 国内自拍亚洲| 国产精品对白刺激久久久| 欧美一级视频精品观看| 亚洲美女色禁图| 欧美激情欧美激情在线五月| 欧美成人午夜激情在线| 猛男gaygay欧美视频| 国产精品久久久久久久久果冻传媒| 一区二区高清视频| 亚洲电影免费观看高清| 久久精品二区亚洲w码| 亚洲欧美视频在线| 亚洲欧美日韩另类精品一区二区三区| 在线观看一区视频| 欧美承认网站| 欧美日韩高清一区| 国产精品免费区二区三区观看| 国产精品久久久久999| 国产美女精品视频| 亚洲第一综合天堂另类专| 亚洲精品在线三区| 欧美在线视频在线播放完整版免费观看 | 亚洲激情第一页| 亚洲欧美日韩在线不卡| 亚洲综合第一| 国产欧美日韩视频一区二区三区| 99热在这里有精品免费| 亚洲视频在线观看| 免费观看亚洲视频大全| 久久精品二区| 国产精品稀缺呦系列在线| 亚洲国产小视频| 久热综合在线亚洲精品| 亚洲精选视频免费看| 欧美日韩一区二区免费在线观看| 日韩视频精品在线| 久久久另类综合| 亚洲国产99| 久久久久9999亚洲精品| 亚洲国产裸拍裸体视频在线观看乱了中文| 亚洲一级二级| 日韩视频一区二区三区在线播放免费观看 | 国产精品国产三级国产aⅴ无密码| 99精品国产福利在线观看免费 | 午夜精品在线视频| 亚洲欧美亚洲| 欧美风情在线| 一区二区三区视频观看| 欧美jizz19hd性欧美| 午夜精品久久久久| 国产欧美日韩免费| 欧美日韩视频在线一区二区观看视频| 韩日精品中文字幕| 亚洲精品国产精品国自产观看浪潮 | 美女久久一区| 欧美日韩一区不卡| 久久久久国产精品麻豆ai换脸| 亚洲国产va精品久久久不卡综合| 午夜在线视频一区二区区别| 国产精品日韩欧美大师| 欧美3dxxxxhd| 久久天堂av综合合色| 欧美一区二区网站| 久久视频免费观看| 亚洲已满18点击进入久久 | 亚洲性感激情| 亚洲视频精品在线| 日韩午夜av| 在线视频精品一区| 亚洲第一福利在线观看| 亚洲欧美文学| 国产一区91| 99re8这里有精品热视频免费| 久久青青草综合| 久久精品噜噜噜成人av农村| 国产精品草草| 午夜精品一区二区三区在线视| 亚洲精品一区二区三区福利| 欧美aa国产视频| 亚洲精品久久久久久久久久久久久 | 久久精品99国产精品酒店日本| 99精品99| 国产私拍一区| 日韩午夜精品视频| 国产精品麻豆欧美日韩ww| 久久久久久久久久久久久女国产乱 | 国产真实乱子伦精品视频| 欧美日本在线| 国产视频一区在线观看| 国产精品老牛| 国产精品专区第二| 激情久久影院| 亚洲精品国精品久久99热| 亚洲高清在线| 亚洲男人av电影| 亚洲图片欧美一区| 老鸭窝毛片一区二区三区| 欧美三区美女| 欧美人在线观看| 亚洲免费视频观看| 久久精品首页| 久久久久国产一区二区| 午夜影院日韩| 亚洲国产精品第一区二区| 性刺激综合网| aa日韩免费精品视频一| 欧美日韩亚洲91| 禁断一区二区三区在线| 亚洲激情视频网| 欧美一区二区| 亚洲一区久久久| 在线播放视频一区| 久久久久天天天天| 欧美国产日韩一区| 久久亚洲综合网| 欧美女激情福利| 亚洲永久免费| 久久久久久久一区二区三区| 亚洲欧美成人一区二区在线电影 | 精品福利电影| 久久黄色网页| 免费成人你懂的| 亚洲国内在线| 欧美精品乱码久久久久久按摩| 亚洲精品一区二区三区不| 亚洲自拍三区| 激情综合电影网| 免费成人毛片| 一本久久知道综合久久| 亚洲在线播放电影| 国产视频一区免费看| 久久在线免费视频| 夜夜嗨av一区二区三区中文字幕| 欧美在线播放一区| 亚洲经典自拍| 国产欧美欧美| 欧美激情亚洲国产| 久久久99爱| 亚洲特色特黄| 亚洲国产国产亚洲一二三| 亚洲专区一区| 99精品视频一区| 亚洲国产另类精品专区| 欧美三区不卡| 亚洲在线国产日韩欧美| 亚洲国产另类久久久精品极度| 国产精品毛片va一区二区三区 |