MFC中双缓存机制的使用和实现,程序运行时间计算

1.使用双缓存进行实现绘制图形的 测试用例

void CViewCamera::OnDraw(CDC* pDC)
{
CDocHADMap* pDoc = GetDocument();
ASSERT_VALID(pDoc);


CRect rect;
GetClientRect(rect);


InitMapDC(pDC->GetSafeHdc(), rect);


int idx = theApp.queCamera.GetMemCameraRecent();
if (idx > -1){
::BitBlt(pDC->GetSafeHdc(), 0, 0,\
rect.right - rect.left,\
rect.bottom - rect.top,\
m_hMainMemDC[idx], 0, 0, SRCCOPY);
theApp.queCamera.SetMemIdle(idx);
//RecordTime(time);
}




/
if (!pDoc)
return;
}

void CViewCamera::InitMapDC(HDC hDC, RECT& stDrawRect)
{
if (m_bInit){
return;
}
for (int i=0; i<MAX_MEM; i++){
//create DC for main map
if(m_hMainMemDC[i] == NULL){
m_hMainMemDC[i] = ::CreateCompatibleDC(hDC);
}


int factor = 3;
int width = (stDrawRect.right  - stDrawRect.left)*factor;// + DEF_BMP_MARGIN * factor;
int height = (stDrawRect.bottom - stDrawRect.top)*factor;//  + DEF_BMP_MARGIN * factor;


//create bitmap for main map.
if (m_hMainMemDC[i] != NULL){
if (m_hMainBitmap[i] == NULL){
m_hMainBitmap[i] = ::CreateCompatibleBitmap(hDC, width, height);
if (m_hMainBitmap[i] != NULL){
(HBITMAP)::SelectObject(m_hMainMemDC[i], m_hMainBitmap[i]);
}
}
}
}

m_bInit = true;

return;
}

2.

void CViewCamera::InitMapDC(HDC hDC, RECT& stDrawRect)
{
if (m_bInit){
return;
}

for (int i=0; i<MAX_MEM; i++){
//create DC for main map
if(m_hMainMemDC[i] == NULL){
m_hMainMemDC[i] = ::CreateCompatibleDC(hDC);
}

int factor = 3;
int width = (stDrawRect.right  - stDrawRect.left)*factor;// + DEF_BMP_MARGIN * factor;
int height = (stDrawRect.bottom - stDrawRect.top)*factor;//  + DEF_BMP_MARGIN * factor;

//create bitmap for main map.
if (m_hMainMemDC[i] != NULL){
if (m_hMainBitmap[i] == NULL){
m_hMainBitmap[i] = ::CreateCompatibleBitmap(hDC, width, height);
if (m_hMainBitmap[i] != NULL){
(HBITMAP)::SelectObject(m_hMainMemDC[i], m_hMainBitmap[i]);
}
}
}
}

m_bInit = true;
return;
}

3.程序运行时间计算test

#if 1
#define F_PATH "Y:\\mytest.txt"
int main()
{
char name[80],pass[80];
FILE *fp=fopen(F_PATH,"a+");
if (fp==NULL)
{
printf("open file error!\n");
exit(0);
}

//MFC
LARGE_INTEGER freq;
LARGE_INTEGER start_t,stop_t;
double exe_time;
QueryPerformanceFrequency(&freq);//获得计数器的时钟频率
fprintf(stdout,"The frequency of your pc is %d\n",freq.QuadPart);
QueryPerformanceCounter(&start_t);//获得初始值


char cTmp[50];
while(gets(name) && gets(pass))
{
if (strcmp(name,"exit")==0 &&strcmp(pass,"1")==0)
{
break;
}
sprintf(name,"%s \t",name);
sprintf(pass,"%s \n",pass);
fputs(name,fp);
fputs(pass,fp);
}
QueryPerformanceCounter(&stop_t);//获得中止值
exe_time=(stop_t.QuadPart-start_t.QuadPart)/freq.QuadPart;
exe_time*=1000;//获取到毫秒
fprintf(stdout,"Your program excuted time is %fms.\r\n",exe_time);
sprintf(cTmp,"Your program excuted time is %fms.\r\n",exe_time);
fwrite(cTmp,strlen(cTmp),1,fp);

if (NULL==fp)
return -1;
printf("%s\t%s\n",name,pass);
fclose(fp);
fp=NULL;
getchar();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值