游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2299|回复: 5

求助 C++关于数据结构的怪问题!!

[复制链接]

26

主题

39

帖子

78

积分

注册会员

Rank: 2

积分
78
发表于 2008-7-20 09:40:00 | 显示全部楼层 |阅读模式
这里有一段代码:


#include   <iostream>
#include <windows.h>

using  namespace std;

struct  PaletteTable
{
   DWORD  colorref;
  int   index;
// int a;
// int b;
};


void main()
{
      PaletteTable palettetable[256] ;
      for( i=0;i<256;i++)
      {
       palettetable.index=i;
       palettetable.colorref=255*65536;
      }



     int r=255,b=255,g=255;
     DWORD  colorref=255*65536;
     BYTE*   psurf=new BYTE;
     DWORD  time=GetTickCount();
     for(int i=0;i<14400;i++)
     {
           for( int j=0;j<1040;j++)   
           {  
              colorref=b*65536+g*256+r;
              int  index=256/2;
              int  half=0;
         
                if(colorref<=palettetable[129].colorref&&colorref>=palettetable[127].colorref)
              {
                       if(colorref>=palettetable[128].colorref + ((palettetable[129].colorref-                                                                  palettetable[128].colorref)>>1)
                              *psurf=  palettetable[129].index;
                 else if (colorref>=palettetable[127].colorref + ((palettetable[128].colorref-                                                                   palettetable[127].colorref)>>1)
                            * psurf=   palettetable[half+index].index;                       
                else
                          *   psurf=    palettetable[half+index-1].index;

                                               
              }
              else if(colorref<=palettetable[128].colorref)
                      index=index>>1;
                else
             {  
                        half+=index;
                             index=index>>1;
             }
                               
        }
  
    }
          cout<<GetTickCount()-time;
}


注意:你不需要知道这一大段代码是做什么的,
你只需要知道cout<<GetTickCount()-time;  显示以上的代码所花的时间,
和PaletteTable这个结构

在这里我要说的是PaletteTable这个结构,注意变量a,b;当我把a的注释去掉以后,花费的时间变多,
当我再把b的注释去掉以后,花费的时间又变正常了,这是为什么?
变量a,b我从来没有用过,为什么会出现这种情况?

15

主题

2093

帖子

2093

积分

金牌会员

Rank: 6Rank: 6

积分
2093
发表于 2008-7-20 10:47:00 | 显示全部楼层

Re:求助 C++关于数据结构的怪问题!!

去掉a的注释后是12字节,再去掉b的注释后是16字节

26

主题

39

帖子

78

积分

注册会员

Rank: 2

积分
78
 楼主| 发表于 2008-7-20 11:34:00 | 显示全部楼层

Re: 求助 C++关于数据结构的怪问题!!

12字节和16字节对访问速度有什么区别

35

主题

1735

帖子

1739

积分

金牌会员

Rank: 6Rank: 6

积分
1739
QQ
发表于 2008-7-20 13:35:00 | 显示全部楼层

Re: Re: 求助 C++关于数据结构的怪问题!!

9940007: Re: 求助 C++关于数据结构的怪问题!!

12字节和16字节对访问速度有什么区别

对齐呗

0

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2008-7-21 19:24:00 | 显示全部楼层

Re: Re: Re: 求助 C++关于数据结构的怪问题!!

ricohcanon: Re: Re: 求助 C++关于数据结构的怪问题!!


对齐呗


我觉得还有些疑惑。非常不解......

他没有加#pragma pack(push,1),而这里实际上每个成员已经对齐了。

难道 12=4*3 和 16=4*4 有大的速度区别?

1

主题

40

帖子

46

积分

注册会员

Rank: 2

积分
46
发表于 2008-7-25 14:01:00 | 显示全部楼层

Re:求助 C++关于数据结构的怪问题!!

如果真是速度差别很大  那楼主可以考虑换电脑
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-21 22:13

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表