Basic C++ optimization question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Basic C++ optimization question

Post by Midnight »

I'm a tutorial taught coder so I don't really know alot of what is proper.

Code: Select all

//example 

int a = 1600;

driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
driver->draw2DRectangle(SColor(0,0,0,0)rect<32>(0,a,0,a))
Would it be wise to attempt to optimize this way?

Does it actually save compile time or system resources or make any difference at all to do something like this?

If "int a" were removed and 1600 put in it's place I mean.

At least 50 charactors would take the place of "a".
Jarlaxle
Posts: 10
Joined: Wed Jul 06, 2005 6:43 am

Post by Jarlaxle »

The point of using a local variable is readability in most cases. Also, in your example, if you wanted to change the value from 1600 to 1500, you can simply change the variable instead of all the hard-coded numbers.

From a performance point of view I don't see much point in using the variable. The compiler has to allocate the memory for an int, but that's not that much effort. What might help optimize your code is declaring the variable as const, as the compiler might be able to do something with that. Good compilers will even automatically use the constant value for const variables, which should improve execution time.

But I'm not an optimization expert, maybe someone will contradict me here.
There are only 10 kinds of people - those that know binary and those that don't.
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

I'm not an optimization expert either, but the absolute fastest way (though there'll be no noticeable difference in almost all cases) is simply to use the literal 160, no variables involved. Of course, if computations of other numbers are needed to arrive at the 160 value, then it is of course faster to do the computations once and store the result, 160, in a variable. As Jarlaxle pointed out, assigning 160 to a const variable allows you to change the 160 value in one place and it will be changed everywhere it's used. This is of course not an optimization, but a convenience.

If you are drawing the same rectangle 10 times, most people would use a for loop. Having it unrolled as you do is the absolute fastest way, but unless it resides in a speed critical inner section of code, there will be no noticeable difference at all, and (I beleive) most optimizing compilers can unroll loops with a fixed number of iterations anyway. In fact, though this is slightly unrelated, in Cg under certain profiles for older hardware with lmore limited gpu's, only loops which can be unrolled by the compiler are allowed.
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

use a code profiler and find the slow bits. Dev-C++ has a nice profiler, look it up in the help.
in that code, calls to "SColor()" and "rect<s32>()" will take more time than reading integers, so this would probably be fastest-

// do this once
SColor a = SColor(0,0,0,0);
rect<f32> b = rect<f32>(0,1600,0,1600);

// do this a million times
driver->draw2DRectangle(a,b);
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Thanks guys very educational.

This was only an example I'm looking to optimize guice and it have simular code is all I'm in no need of a million squares though but thanks.

wonder if that would crash Irrlicht..might have to try it. 8)
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Using a literal value in that many cases is actually slower. Using the variable is faster.

The way function calls are handled, very basically, is that variables are passed around. Let's say you have a function call like so :

func( int a, int b );

If you call it like this :

func( 1, 2 );

The system will actually allocate memory for the arguments on the stack, and then pass them to the function call, by copying their data into the local variables the function will use.

If you called the function like so :

int a = 1;
int b = 2;
func( a, b );

The system will now simply copy the values from the already allocated memory for a and b variables to the local variables for the function.

Now, the fastest way to make the function call, is like so :

func( const int& a, const int& b );

Now, you still call the function like :

int a = 1;
int b = 1;
func( a, b );

However, since you are using references, the variables a and b are used inside the actual function, no memory allocation is done, inside the function or on the stack to generate the temporary memory for literal values.

Their are many, many, many more details to all of this, and why certain cases are better than others, however, using literal values is actually the slowest code you could use.
Image
Post Reply