Skip to main content
gamedev.net
🔒 Locked

Optimisation Tips anyone?

Started by SpaceDude Jan 27, 2004 at 10:43 AM 5 replies 1.4k views
Original Post
SpaceDude
SpaceDude
I''m trying to get my rendering code to go as fast as possible, and i was wondering if anybody has tips on how to optimise my code or an article would be cool Basically i''ve got a few functions that are getting called about 125 000 times per frame. Should i be doing things like using "inline" and "static"? i''m not sure what exactly they do and when i should be using them. I noticed i jumped from about 100 fps to 300 fps by changing from something like this (its just an example):
for(int i=0; i<125000; i++)
{
     MyFunction(i);
}

void MyFunction
{
     if (!m_pArray)
          return;
     ... rest of my code ...
}
 
to
for(int i=0; i<125000; i++)
{
     if (m_pArray[i])
          MyFunction(i);
}

void MyFunction
{
     ... code ...
}
 
I had a lot of empty spaces in my array, so that part of code was getting called a lot... Anyway, this leads me to beleive that it is more efficient to bunch all my code in the loop rather than calling other functions... This seems rather messy though, is there something i can do here to speed it up (i.e. without cut and pasting my function into the loop)? Allocating new memory also seems to be very slow, i noticed that using a fixed size array was much faster than using a linked list (although i really need the ability to dynamically change the size of my data :/). Thanks.
_the_phantom_
_the_phantom_
quote:
Original post by SpaceDude
Should i be doing things like using "inline" and "static"? i''m not sure what exactly they do and when i should be using them.



If you''re getting tripped up on things like this then you should probably learn how to use the language first before worrying about speed

However, if you want to go ahead and improve the speed then profile, profile, profile.
Doing so will tell you where you NEED to speed things up and thus you know were to focus your efforts.

You are pretty much along the right track with removing redunant function calls and not allocating ram in critcal loops, however keeping your code readable is also important, if you mangle your code up now, when i get the feeling you are still some way from a ''finished'' program, it will be harder for you to add things later.

As the old addage goes : Make it work, make it work right, make it work fast.
Once your game/enginer/render does the first two THEN worry about the third one some more.
extralongpants
extralongpants
inlining a function basically allows the compiler to cut and paste the code where you called it, so it gets rid of the function overhead. The inlined functions have to be small though, or the compiler wont inline them.
In terms of dynamic arrays, i would suggest something like this:

void * array[ 50000 ];
int numUsed;

void AddItem(item)
{
array[numUsed] = item;
++numUsed;
}

void DelItem(itemID)
{
array[itemID] =array[numUsed-1];
--numUsed;
}

this allows you to alter the size of the array and add and remove items whenever you want, without actually allocating any more memory.
of course, this example was REALLY simple. you''ll probably have to test whether or not your going to go past the array when you add an item and what not.



-----------------------------
I never chew my cabbage twice
Reverse_Gecko
Reverse_Gecko
Definatly inline some of the inner functions that gets called billions of times, all the stack pushes and pops add up.

Also make any vars that get used a hell of a lot register variables. Don''t make very many though.
Jeff Bland (Reverse_Gecko)reversegecko@gmail.com
mishikel
mishikel
odiusangel, that''s the textbook definition of inline. Guru of the Week #33 at http://www.gotw.ca/gotw/033.htm and the article "Inline Redux" in the Novemeber 2003 edition of the C++ User''s Journal (http://www.cuj.com/documents/s=8906/cujexp0311sutter/) are two very good reads. I''ve found that determining the benefits of inlining a function is pretty tricky business. First, you have to verify that the function is actually inlined by the compiler. Then you have to determine, through profiling, if there is any speed difference. Then you have have to weigh the difference in speed against the increased coupling that inlining brings to your code. Plus a couple of other factors. (Is code size more important than code speed? It might be.)

High level algorithmic optimizations will give the greatest benefits. To the OP, don''t worry about inline. Rather, concentrate on improving the O(n) nature of your code.



Matt
SpaceDude
SpaceDude
Thanks for the tips guys, i think that the first thing i need to do is get a profiler... I''m using MS Visual Studio .NET, and i''m pretty sure it doesn''t include a profiler? Can someone recommend a good profiler? One website suggested http://developer.intel.com/vtune/analyzer/ so i''ll check that out.

I need to get this code running as fast as possible because i''m only getting between 100 and 400 FPS in a very small test map (depending on where i look). So there is not much point in programming the rest of the game if i''m not gonna get a decent frame rate on a big map.

I''m not a newbie programmer, i just haven''t had much need for speed optimisations until now.
Agony
Agony
AMD Code Analyst

Haven''t used it yet, and don''t know if it works with all x86 processors, but it is free, so it might be worth checking out.

And I believe Intel''s VTune is way expensive.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.