0
点赞
收藏
分享

微信扫一扫

随想录(canvas双缓存下的性能分析)


    有过canvas编程经验的同学都知道,如果希望在客户端屏幕上不出现闪烁的情况,最好使用双缓存输出的方法。所谓的双缓存,就是在paint画面之前,先用memdc将所要表现的内容全部弄好,最后再bitblt到dc上面。这种方法很实用,但是效率究竟差多少,却很少人做过实验,今天我们就来试试看。

1、原始画面

    为了便于测量,我们特地在代码中多次打印和输出字符,

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;

val = ::GetTickCount();
for(i = 0; i < 10000; i++){
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
}
val = ::GetTickCount()-val;
EndPaint(hWnd, &ps);
break;

2、先缓存,后输出画面

    和实验1不同,这里我们先创建一个兼容dc和bitmap,等到打印后再bitblt输出。

case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);

hmemdc = CreateCompatibleDC(hdc);
hnew = CreateCompatibleBitmap(hdc, rt.right-rt.left, rt.bottom-rt.top);
hold = (HBITMAP)SelectObject(hmemdc, hnew);

// TODO: Add any drawing code here...
val = ::GetTickCount();
for (i=0; i < 10000; i++){
DrawText(hmemdc, szHello, strlen(szHello), &rt, DT_CENTER);
}
val = ::GetTickCount() - val;

BitBlt(hdc, 0, 0, rt.right-rt.left, rt.bottom-rt.top, hmemdc, 0, 0, SRCCOPY);
SelectObject(hmemdc, hold);
DeleteObject(hnew);
DeleteDC(hmemdc);

EndPaint(hWnd, &ps);
break;

3、实验结果

    我们是按照多次打印统计的方法来分析的。用来统计的函数是GetTickCount函数,单位是毫秒。实验结果是,实验1中的val数值为210ms,实验2中数值为140ms。这里只是打印了字符一种情况,可能效果不太明显。我想,如果是点阵这种情况的话,可能更体现出兼容dc的优越性。


举报

相关推荐

0 条评论