Skip to main content
gamedev.net
🔒 Locked

Some optimizations....

Started by MattS423 13 replies 400+ views
Original Post
MattS423
MattS423
ok, what''s faster: 1) and if--else if statement with an == 2) an if--Else if with a logical OR 3) 3 &''s (against varibles), and 2 Bitshifts (against varibles) I think it''s number 3, but i''m not sure also, if anybody can help me optimize this: index and LineLegnth are floats: RedCurr is an int
for(/*some stuff withindex*/)
{
//...
RedCurr = (int)((index/LineLegnth)*(RedDelta) + RedStart + 0.5f);
//...
}
 
I know the real killer here is the conversion from floats to ints....any ideas? (RedCurr must remain an int because of pixel ploting) I''m doing line shading if it matters... tanks guys!
Programmers of the world, UNTIE!
civguy
civguy
quote:

ok, what's faster:
1) and if--else if statement with an ==
2) an if--Else if with a logical OR
3) 3 &'s (against varibles), and 2 Bitshifts (against varibles)


Depends on the system you're using. You mean something like:

  
1) if (a==b) {} else {}
2) if (a || b) {} else {}
3) ((a&b&c)<<d)<<e

?
My quess is that 1 and 2 are equally fast and faster than 3. 1 and 2 don't require much computation and jump prediction will make the jump cost neglible (depends on how often the equation varies between true and false, though)

  
for(/*some stuff withindex*/)
{
//...

RedCurr = (int)((index/LineLegnth)*(RedDelta) + RedStart + 0.5f);
//...

}

Perhaps you can use LUT here (dunno if applicable):

  
for(/*some stuff withindex*/)
{
//...

RedCurr = RedLUT[index];
//...

}
It would be fastest.

[edited by - civguy on February 27, 2003 2:27:27 PM]
MattS423
MattS423
I''m thinking:


  
if(A | B) //could be if(A == B)

{
Buffer[(y*pitch)+x] = ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
}

else if (A | C) /could be else if(A==C)
{
Buffer[(y*pitch)+x] = ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
}


or i could cut all that and just do...


Buffer[(y*pitch)+x] = ((b & 31) + ((g & GreenAnd) << 5) + ((r & 31) << RedShift))


where GreenAnd & RedShift are pre-calculated.

This is all in a loop, thats in a loop, thats in a loop, by the way....

Whats LUT? never heard of it...
Programmers of the world, UNTIE!
Anthracks
Anthracks
LUT = Look-Up Table. Pre-calculate the value of the equation for each possible value of index and stick it in an array when the program starts up, so rather than doing the calculation each time you''re just doing an array access.
civguy
civguy
quote:
Original post by MattS423
Whats LUT? never heard of it...
A look-up-table. Just like my code shows: a precalculated array where you can get the correct values given an index.

  
Buffer[(y*pitch)+x]

Potential slow down (unless x and y totally random). Use an index that keeps rising instead of doing multiplication all the time:

  
for(y=0;y<yy;++y)
for(x=0;x<xx;++x)
Buffer[y*pitch+x] = 2;

=>

  
int index = 0;
for(y=0;y<yy;++y) {
for(x=0;x<xx;++x) {
Buffer[index++] = 2;
}
index+=pitch-xx;
}

And then:

  
for(...) {
if(A | B) //could be if(A == B) {

Buffer[(y*pitch)+x] = ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
} else if (A | C) {
Buffer[(y*pitch)+x] = ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
}
}

My quess is that A|B and A|C remain the same throughout the whole loop. Move the if clauses out of the loop like this:

  
if(A | B)
for(...) {
Buffer[(y*pitch)+x] = ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
} else
if (A | C)
for(...) {
Buffer[(y*pitch)+x] = ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
}

But that's real fine tuning, probably won't give you anything.(maybe a 1% speedup if you're lucky)

[edited by - civguy on February 27, 2003 2:51:27 PM]
MattS423
MattS423
Humm....but if i did a Look up table, i''d still wind up doing the same number of multiplys, and even then i''d still have to change the float to an int! So with the time it would take to do an array lookup, then it wouldn''t save me any time at all.

humm...i''m reminded of brensenhams int-only line drawing algorithim...could something like that be applied here? (maybe?)
Programmers of the world, UNTIE!
civguy
civguy
quote:
Original post by MattS423
Humm....but if i did a Look up table, i'd still wind up doing the same number of multiplys, and even then i'd still have to change the float to an int! So with the time it would take to do an array lookup, then it wouldn't save me any time at all.
No.. You calculate the look-up table only once, at program start-up. And then in the loop you only have to look up a value from the array, no matter how many times you run the loop.

[edited by - civguy on February 27, 2003 4:46:51 PM]
63616C68h
63616C68h
It doesn''t matter. Here, if you want sleek, this bitch is what you want:

  
//assignment:

c = a>b ? 3 : 4;
//if a is greater than be, then the ternary

//expression assigns 3 to c, else c equals 4.

a==b?/*then*/:/*else*/
Keep coming back, because it's worth it, if you work it, so work it, you're worth it!
MattS423
MattS423
thats great, 63616C68h , but how does that help me?
Programmers of the world, UNTIE!
Pragma
Pragma
Float-to-int casting is brutally slow (I think this is specific to msvc). You should do a quick google search for an assembly language ftol() function, or use a compiler which doesn''t have this issue.

"Math is hard" -Barbie
"Math is hard" -Barbie
civguy
civguy
quote:
Original post by 63616C68h
It doesn''t matter. Here, if you want sleek, this bitch is what you want:
That''s not optimization. And often that''s just obfuscation.

MattS423
MattS423
if MSVC++ 6.0 has a problem converting floats to ints, and it''s the complier''s problem, then why don''t they have an update or something?

wait a second...what happends if i just don''t cast it?
Programmers of the world, UNTIE!
civguy
civguy
quote:
Original post by MattS423
wait a second...what happends if i just don''t cast it?
It will be implicitely casted. But please try the look-up table solution..

civguy
civguy
double trouble..

[edited by - civguy on February 27, 2003 5:18:43 PM]
MattS423
MattS423
I got it!

I simply used ints and applied a method similar to brensenhams...

there are still floats, but i never convert them to ints, except once on the startup.
Programmers of the world, UNTIE!

Topic Locked

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

Sign in to reply to this topic.