   Global vs Structure Variables -Which is Faster?
  I''m not a newbie, but this is somewhat of a newbie question.If I were to be in need of a variable that will be accessed like mad throughout the entire running of my project, down in the color-component pixel level code of blits (eg. like mad),which would be faster?struct GraphicsEngine{ULONG TransColor;........};or justULONG TransColor; <- This being a normal global variable.So the accesses would be different in this way:for(i=1billion; i>0; i--)SomePixel = Table[GraphicsEngine.TransColor]; yada yadaorfor(i=1billion; i>0; i--)SomePixel = Table[TransColor]; <- TransColor as globalI don''t know because I''ve heard that globals are not as quickly accessed as stack variables. I don''t understand why. Would it be true even if a variable is contained in a globally declared class or structure? Also, I''ve heard that the dot (.) and "member access" pointer operators take some clock ticks too. I don''t want either, but I don''t see how I can get away from both.Thanks for any advice or theories.Jiia 
  You are talking about retarded optimisation here. Just do what you prefer to do. The speed increase is so minimal you are better off hitting your head against the wall, or popping ecks. Or something. ;  Like Captian Goatse has said, if there was a speed difference then it would be unnoticeable. So just do it the way you are most comfortable with. ;  Okay then, don''t humor me. I didn''t say I was stressing out about it, I''m just curious. Saying "I don''t know" would have been sufficient.Jiia ;  We are not humoring you, we are just trying to help. ;  Hi JiiaI''m not sure but I think there is no difference in executionspeed between the two methods you''ve mentioned.If the instance of your Struct is a global Variable the compilerknows it''s location on the stack and can "hardcode" the member access in the .exe file.Same goes with the single global variable, the compiler already knows its location.Global variables ARE on the stack but if you have a pointer and allocate let''s say 10kb with malloc() those 10kb are on the HEAP and this is slower.I maybe wrong,whatever I hope I didn''t confuse you.Why don''t you try both methods and do a little profiling...   --::[Madhed]::-- ;  Maybe it's a little too early to be asking for much. The idea is that I want you to humor me.Perhaps if someone knows assembly, they could explain the few clock cycles used for either, or even explain why globals are slower than stack. Or any single question I asked above. Brief explainations are very welcome.I guess the optimization is retarded, if you were using hardware for your graphics routines, as I expect you do with OpenGL. I'm writing my own, and I want them to take every retarded optimization into account.Thanks again for any helpJiiaEDIT - Thanks Madhed, thats what I needed to hear.   [edited by - Jiia on November 1, 2003 5:53:14 AM];  Hey JiiaDo you have MSVC or any similar IDE? Try both methods,debug and switch to deassembly mode (or whatever it is called?)then look at the assembly code and find out if any big differences exist...   --::[Madhed]::--   [edited by - MadHed on November 1, 2003 5:58:26 AM];  Here ya go, a smashed and baked potato:Normal C++ Code: //-------------------------------------------------------------------struct STRUCT{int x;};int GLOBAL_VARIABLE = 0;STRUCTGLOBAL_STRUCTURE= { 0 };//-------------------------------------------------------------------int main(int, char**){int NORMAL_VARIABLE = 0;STRUCTNORMAL_STRUCTURE = { 0 };int*ALLOC_VARIABLE = new int;STRUCT* ALLOC_STRUCTURE= new STRUCT;GLOBAL_VARIABLE = 1;GLOBAL_STRUCTURE.x = 1;NORMAL_VARIABLE= 2;NORMAL_STRUCTURE.x= 2;*ALLOC_VARIABLE= 3;ALLOC_STRUCTURE->x= 3;return 0;}ASM Code: (generated by BCC 5.6) .386pifdef ??versionif ??version GT 500H.mmxendifendifmodel flatifndef??version?debugmacroendmendif?debugS "main.cpp"?debugT "main.cpp"_TEXTsegment dword public use32 ''CODE''_TEXTends_DATAsegment dword public use32 ''DATA''_DATAends_BSSsegment dword public use32 ''BSS''_BSSendsDGROUPgroup_BSS,_DATA_DATAsegment dword public use32 ''DATA''align4_GLOBAL_VARIABLElabeldworddd0align4_GLOBAL_STRUCTURElabelbytedd0align4$ebembaialabelbytedd0_DATAends_TEXTsegment dword public use32 ''CODE''_mainsegment virtual@_mainprocnear?live16385@0: ; ;int main(int, char**) ;push ebpmov ebp,esppush ecxpush ebx ; ;{ ;int NORMAL_VARIABLE = 0; ;STRUCTNORMAL_STRUCTURE = { 0 }; ;@1:mov eax,dword ptr [$ebembaia]mov dword ptr [ebp-4],eax ; ; ;int*ALLOC_VARIABLE = new int; ;push 4call @$bnew$quipop ecxmov ebx,eax ; ;STRUCT* ALLOC_STRUCTURE= new STRUCT; ;?live16385@48: ; EBX = ALLOC_VARIABLEpush 4call @$bnew$quipop ecx ; ; ; ;GLOBAL_VARIABLE = 1; ;?live16385@64: ; EBX = ALLOC_VARIABLE, EAX = ALLOC_STRUCTUREmov dword ptr [_GLOBAL_VARIABLE],1 ; ;GLOBAL_STRUCTURE.x = 1; ;mov dword ptr [_GLOBAL_STRUCTURE],1 ; ; ;NORMAL_VARIABLE= 2; ;NORMAL_STRUCTURE.x= 2; ;mov dword ptr [ebp-4],2 ; ; ;*ALLOC_VARIABLE= 3; ;mov dword ptr [ebx],3 ; ;ALLOC_STRUCTURE->x= 3; ;?live16385@128: ; EAX = ALLOC_STRUCTUREmov dword ptr [eax],3 ; ; ;return 0; ;?live16385@144: ; xor eax,eax ; ;} ;@3:@2:pop ebxpop ecxpop ebpret @_mainendp_mainends_TEXTends_TEXTsegment dword public use32 ''CODE''_TEXTendspublic_GLOBAL_VARIABLEpublic_GLOBAL_STRUCTURE extrn __setargv__:near extrn @$bnew$qui:near?debugD "main.cpp" 12129 28219endHappy Hunting!;  Well anyway, there is a huge difference. This is how I set it up.// GlobalsULONG VarA;XgStruct VarB; // Structure with a few ULONG variablesSomeFunction(){LONG Test = 1;// thisfor(INT x=0; x<99999; x++)  VarA = Test;// or thisfor(INT x=0; x<99999; x++)  VarB.Num = Test;}Please take the 99999 losely, as I don''t remember the exact number I used. The VarB.Num yanked me to 41 FPS, while the other passed up my refresh rate (85 FPS).People keep telling me these are stupid optimizations, but I believe this proves them wrong. Other people also tell me that the compiler will optimize these things for me, but they are also wrong. So assigning a temporary local variable to the value of a structure.member variable before a hardcore loop is a must for me.Jiia 