Page 4 of 4

Re: Game Optimization Tips

PostPosted: Tue May 06, 2008 6:36 pm
by Fuzzy
Bee-Ant wrote:There are 5 lines. If there are more lines, will it affect the game file size???


Code: Select all
if(canjump==1)       // line 1
{                            // line 2
    yvelocity=-10;     // line 3
    canjump=0;        // line 4
}     


will make a game the exact same size as

Code: Select all
if(canjump==1){ yvelocity=-10;canjump=0;}


will make the exact same size as
[code]
if
(
canjump
==1
)
{
yvelocity
=-10;
canjump
=0;
}

The formating of the raw code has no effect on the game size.

only extra operations will add size to your game.

Re: Game Optimization Tips

PostPosted: Sun Oct 09, 2011 6:25 am
by EvanBlack
Necrobumping for importance!

More tips should be added, this should be something like a sticky.


On that last thing that was said so many years ago..

Every mathmatical calculation that has to be done in the in code is what increases the size of the executable itself. For every time it has to add, subtract, multiply, or divide.

Meaning:

Code: Select all
while( x != 100) { ++x;)


is more resource friendly than

Code: Select all
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
//ect 100 times..


Because the first one just puts this function in the machine code:

ADD 1 -> X;
Jump back one space in memory until X = 100;


while the second actually adds that one ADD line, one hundered times into the memory.


So as it was said, the more automation is better, not only does it save YOU time, but it saves valuable resources too.

Sometimes what may seem like sufficient code is really taxing on the cpu because it has to repeat the same task multiple times even though it could have just jumped back to a line in the memory and started there again rather then streaming through and reloading its cache.

Remember that your program goes through this process:

CPU->Cache->CPU->North Bridge->Ram->North Bridge->CPU->Cache->CPU->Cache->CPU->Cache->CPU->North Bridge->Ram->North Bridge->CPU->Cache->CPU->North Bridge->South Bridge->internal components->External Components

Sometimes repeating its path back and forth from the CPU to RAM thousands of times, sometimes even heading to the Hard Drive to be stored in the page file then be picked back up into the RAM.

So the more equations your program makes the more work your cpu does.

Re: Game Optimization Tips

PostPosted: Thu Oct 13, 2011 8:34 am
by Fuzzy
EvanBlack wrote:Necrobumping for importance!

More tips should be added, this should be something like a sticky.


On that last thing that was said so many years ago..

Every mathmatical calculation that has to be done in the in code is what increases the size of the executable itself. For every time it has to add, subtract, multiply, or divide.

Meaning:

Code: Select all
while( x != 100) { ++x;)


is more resource friendly than

Code: Select all
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
x = x +1;
//ect 100 times..


Maybe. It depends on the compiler. Some will optimize that. Also placing the ++ before the variable isnt any faster. At one time it was, but modern compilers all know that trick. They'll switch it for you. the gains are tiny in any case.

Testing it: GE does not roll multiple x = x + 1; lines into a loop. But many compilers like GCC and windows will.

Re: Game Optimization Tips

PostPosted: Thu Oct 13, 2011 11:47 am
by Game A Gogo
are while loops more efficient than for loops?
It probably depends on what needs to be achieved...

Re: Game Optimization Tips

PostPosted: Fri Oct 14, 2011 12:26 am
by EvanBlack
I would always use a for loop over a while loop. The problem with while loops is that they can become infinite if not cared for properly and then they will destroy a program.

while loops are best used for loop that don't iterate but instead continue until statement is true, such as a game loop:
Code: Select all
while(!exit)
{
    //gamecode
}


This kind of loop is the only one you can have here.

Another version of the while loop is the do-while. This kind of loop will always run at least once.

But an example of a while loop that is out performing a for loop, but still a for loop could do the same thing:

Code: Select all
int x = 0;
value = 12; // this variable will always hold values >= 3 and are divisible by 3;

while( x < value)    //This runs 4 times
{
    function(x); ++X;
    function(x); ++X;
    function(x); ++X;
}

   //this runs 12 times, checking value each time
for(x = 0; x < value; ++x) //the for loop in this case has to reset the value 0,
                              //added processing time, it doesn't have to always.
{
   function(x);
}


//Bug in while loop shows a possible how it could end up running into infinity if X was initialized as a global variable.


Here is another for loop that is more resource friendly to the c script:

Code: Select all
int x = 0;
int value = 12;

for(x; x < value; x++)
{
     function(x); x++;
     function(x); x++;
     function(x);
}


As you can see this for loop is more resource friendly than the previous for loop, and it will not run into infinity.

Re: Game Optimization Tips

PostPosted: Fri Oct 14, 2011 12:43 am
by skydereign
It really depends on how your write the loops. Both loops can go on infinitely, and both loops can be used in the same ways, but for loops are built for iterative looping (though they can be used in other cases). So you can write more efficient for loops or more efficient while loops. Generally though, it makes more sense to look at an iterative loop if it is a for loop, and an until true loop as a while, like EvanBlack said.