Powers vs Multiplying

BlitzMax Forums/BlitzMax Programming/Powers vs Multiplying

I just ran some test..seems Powers are a bit on the slow side. They seem to remain constant in execution regardless of the power size. I would think powers would be more optimized than this.



Vari1 = 2
Local Counter1, Counter2
Local PH
Local EndTime

StartMilli = MilliSecs()
EndTime = StartMilli + 1000

Repeat
	Counter2:+1
	
	PH = Vari1^10

Until EndTime < MilliSecs()

Print "^" + Counter2



StartMilli = MilliSecs()
EndTime = StartMilli + 1000

Repeat
	Counter1:+1
	
	PH = Vari1 * Vari1 * Vari1 * Vari1 * Vari1 * Vari1 * Vari1 * Vari1 * Vari1 * Vari1

Until EndTime < MilliSecs()



Print "*" + Counter1

Delay 100

Print Counter1/Float(Counter2)


End


Problem is: Did you ever try to create a power of 2.72343 in your way? ;-)

It ends up constant becase a^b = a * ln(b) on which ln is the worse and calculation intense part.
If you defined your variables correctly (with type not this sluggish B3D style), you could have used :float which would be faster, because yours uses int which ends with double calculation. Gives more accurate results but takes its time.

I always use * if I know the power and if the power is not > 4 (got used to that in B3D where the multiplication is faster for a^x with x < 4)

Well, for one thing...

a^b = a * ln(b)


I don't think that's correct.

For another thing, Pow takes into account fractional numbers and not just integers, so it's more complex than simply multiplying a number by itself n times, hence why it takes longer.

Just thought I'd add that in case you didn't understand Dreamora's gobbledegook.

What do you think is not correct?
The equation itself is.
The actual implementation might differ, since ln is the slowest mathematical operation with non-matrices. but still the implementation bases on this and uses IEEE optimations which use the restriction correctness of the types to gain in speed (as SQRT does as well)

So you're saying that 5^2.5 = 5*ln(2.5), right?

If so, my calculator disagrees.

5^2.5 = e^(ln(5^2.5)) = e^(2,5 * ln(5)) = 5 * e^2.5

oops yeah you are right, was wrong, mixed it vice versa :-(

.

There is no reason why the compiler can't replace x^c with ( x * x * ... x ) for sufficiently small values of c. Using multiplication is O( n ) and calculating with exp and ln is constant, but a very large one.

Um, no, it's still wrong. You can't jump from

exp( 2.5 * ln( 5 ) ) -which is right
to
5 * exp( 2.5 ) -which is wrong

narv yeah its monday morning and boring

I stop telling strange stuff thats crap.