• ### Popular Now

• 13
• 15
• 27
• 9
• 9

#### Archived

This topic is now archived and is closed to further replies.

# Optimize this ecuations

This topic is 5738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how can i optimize this?: // 139 cycles x = 0.5*(1 + c); x = 0.5*(x + c/x); x = 0.5*(x + c/x); x = 0.5*(x + c/x); c is a constant thnx

##### Share on other sites
Are they floats or integers? What does this accomplish? Form there I can probably help...

I've figured out what it does, and I ahve a better question: why are trying to optimize somethign that approximates 1? That's completely stupid... Why not just use 1 and not a fancy set of equations that eat cycles like there's no tommorow?

x = 1;

[edited by - puzzler183 on June 30, 2002 12:46:58 PM]

##### Share on other sites
precalculate c/x and then use it - you get 1 instead 3 divs
if using int. values change (blah)*0.5 to (blah)>>1 (shift instead mul)
write in asm

##### Share on other sites
It doesn''t aproximate one! It calculates a square root!
Maybe you should check the algo with different constants than 1

I don''t know anyway to optimise this except defining c and x as ''register'' (assuming you use c/c++), so acess to these two vars is as fast as possible. You could also calculate the difference between the two latest iterations and then quit the loop if you got enough precision.

Yesterday we still stood at the verge of the abyss,
today we''re a step onward!

##### Share on other sites
quote:
Original post by MirekCz
precalculate c/x and then use it - you get 1 instead 3 divs
if using int. values change (blah)*0.5 to (blah)>>1 (shift instead mul)
write in asm

you cannot precalculate c/x as it changex with every iteration...

Yesterday we still stood at the verge of the abyss,
today we''re a step onward!

##### Share on other sites
yes, is the square root the numbers are floats, and i cant precalculate c/x because it changes between equations.
Maybe i can aproximate 1/x for c/x and convert it in c*aprox, but i dont know how aproximate 1/x.

##### Share on other sites
maybe in optimized fpu asm is more fast, but the fpu asm that i write is slow (too many instructions for do something)

##### Share on other sites
Uh ok. With 4 repititions it gets close to one. You will ned about 1000...

##### Share on other sites
I''ve tried something like:
_asm
{
fld c
fsqrt
fstp x
}
and it''s slower than standard sqrtf(), so I honestly doubt if it''s possible to beat the standard library.
Maybe someone has another suggestion?

##### Share on other sites
Puzzler183, with 4 repetitions not get closer to 1, get closer to sqrt of c,
p.e. c=5.2

x = 0.5*(1 + 5.2) = 3.1
x = 0.5*(3.1 + 5.2/3.1) = 2.3887
x = 0.5*(2.3887 + 5.2/2.3887) = 2.2828
x = 0.5*(2.2828 + 5.2/2.2828) = 2.2803

sqrt(5.2) = 2.2803

get a calculator and test it