随笔-59  评论-36  文章-0  trackbacks-0

近来遇见了这样一个问题:
显示界面时需要将一个Gdiplus::Bitmap对象的内容贴在界面上,而其中的内容又是不停的变化的,于是问题来了,我是对Bitmap对象通过调用Graphics::Clear重置呢,还是把这个Bitmap对象搞成局部变量每次进来时都新建一个呢?

傻X的我起初一直以为通过Clear函数重置Bitmap要比调用Bitmap的构造函数新建快,所以我尽力重复利用已有的Bitmap对象而避免重建,但昨天测试了一下发现新建要比重置快得多

这是我的测试方法: 对比重复创建一个100×100的Bitmap和重置同样尺寸的Bitmap对象所用的时间

环境
  系统:XP SP3
  CPU:AMD Athlon II X2 245      2.96G
  内存:2G
  环境:VC 2008 sp1 

    {
        CString tmp;
        SYSTEMTIME   begintime,endtime,passtime;
        ZeroMemory(
&begintime , sizeof(SYSTEMTIME));
        ZeroMemory(
&endtime , sizeof(SYSTEMTIME));
        GetLocalTime(
&begintime); 
        
for (int i = 0 ; i < 100000 ; i++)
        {
            Gdiplus::Bitmap Auto(
100,100);
        }
        GetLocalTime(
&endtime); 


        tmp.Format(_T(
"%dms") , (endtime.wSecond - begintime.wSecond)*1000 + endtime.wMilliseconds - begintime.wMilliseconds);

        MessageBox(tmp);
    }

    {
        CString tmp;
        SYSTEMTIME   begintime,endtime,passtime;
        ZeroMemory(
&begintime , sizeof(SYSTEMTIME));
        ZeroMemory(
&endtime , sizeof(SYSTEMTIME));
        CDC 
*pDC = GetDC();

        Gdiplus::Graphics grap (pDC
->m_hDC);
        Gdiplus::Bitmap   AutoBmp(
100,100);
        Gdiplus::Graphics 
*pGrap = grap.FromImage(&AutoBmp);
        Gdiplus::Color newcolor(
0,0,0,0);
        GetLocalTime(
&begintime); 
        
for (int i = 0 ; i < 100000  ; i++)
        {
            pGrap
->Clear(newcolor);
        }
        GetLocalTime(
&endtime); 
        tmp.Format(_T(
"%dms") , (endtime.wSecond - begintime.wSecond)*1000 + endtime.wMilliseconds - begintime.wMilliseconds);
        MessageBox(tmp);
    }


结果:
当循环1W次时所用时间(单位MS)
             构造函数新建对象     Graphics::Clear重置
                      62                                     344
                      62                                     344
                      47                                     344
                      62                                     343
                      47                                     344
                      62                                     360
                      63                                     344
                      47                                     344
                      62                                     344
                      62                                     343
----------------------------------------------------
平均             57.6                                 345.4

当循环10W次时所用时间
             构造函数新建对象     Graphics::Clear重置
                      563                                   3467
                      562                                   3407
                      578                                   3531
                      563                                   3563
                      625                                   3390
                      578                                   3515
                      562                                   3484
                      594                                   3515
                      547                                   3453
                      563                                   3422
----------------------------------------------------
平均             573.5                                 3474.9


posted on 2010-12-23 09:45 zhaoyg 阅读(2608) 评论(1)  编辑 收藏 引用 所属分类: other

评论:
# re: 对于Gdiplus::Bitmap对象的重置和新建谁快谁慢 2012-06-29 14:42 | as
你可以用纯色填充一下  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理