Friday, September 16, 2005

Re: Code Efficiency - Variable Scope Depth

   int i;
   //code code code
   for(int j=0; j<5; j++)
 mov         dword ptr [j],0
 jmp         main+4Eh (label1)
 mov         eax,dword ptr [j]
 add         eax,1
 mov         dword ptr [j],eax
label1:
 cmp         dword ptr [j],5
 jge         main+64h (later_in_the_program)
   //code code code
   for(i=5; i<10; i++)
 mov         dword ptr [i],5
 jmp         main+94h (label2)
 mov         eax,dword ptr [i]
 add         eax,1
 mov         dword ptr [i],eax
label2:
 cmp         dword ptr [i],0Ah
 jge         main+0AAh (later_in_the_program_again)

Space for local variables is allocated on the stack at the beginning of every function (although they were treated as global in this case, due to the optimizer).  Where you declare them [within the function] is irrelevant.  Many game programmers I know tend to declare them at the beginning of every function, so that every variable can be looked up in one place.  Most other programmers (for some reason), however, seem to declare them as they're used; choose whatever fits you best.

...I did this instead of my Physics, which is due in half-an-hour :D  Muaha

0 Comments:

Post a Comment

<< Home