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!
🔒 Locked
Some optimizations....
Advertisement
Original Post
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
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];
//...
}
[edited by - civguy on February 27, 2003 2:27:27 PM]
I''m thinking:
or i could cut all that and just do...
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...
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...
quote:A look-up-table. Just like my code shows: a precalculated array where you can get the correct values given an index.
Original post by MattS423
Whats LUT? never heard of it...
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]
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?)
humm...i''m reminded of brensenhams int-only line drawing algorithim...could something like that be applied here? (maybe?)
quote: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.
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.
[edited by - civguy on February 27, 2003 4:46:51 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement