1. POWER()
ummmmm.... due to the recent thread on 3d distances,
I kept noticing odd results on various things...
boiled it down to the following:
It seems that power(x,2) is not quite right as far as speed.
It says in library.doc that powers of 2 are calculated very
efficiently. I kept noticing wierd 'lagging' in various things
I was doing, so I drummed up the following benchmark
which compares the times of X*X versus power(X,2):
----------------------------begin pwrbench.ex
include machine.e
sequence x
tick_rate(1000)
procedure bench(integer reps, object val, sequence msg)
object ans
atom now
now = time()
for test=1 to reps do
ans=val*val
end for
printf(1,msg & " X*X timing:%7.3f\n",time()-now)
now = time()
for test=1 to reps do
ans=power(val,2)
end for
printf(1,msg & " power timing:%7.3f\n",time()-now)
end procedure
x={} --generate a sequence of random real numbers
for i=1 to 1000 do x=x & rand(10000)*0.1 end for
bench(5000,x,"real seq")
x={} --generate a sequence of random integers
for i=1 to 1000 do x=x & rand(10000) end for
bench(5000,x,"int seq")
bench(1000000,5000.0123,"real num") --benchmark a million real's
bench(1000000,3000, "int num") --benchmark a million integers
----------------------------end pwrbench.ex
And the results I got were not very close at all...
real seq X*X timing: 4.330
real seq power timing: 18.154
int seq X*X timing: 0.911
int seq power timing: 18.974
real num X*X timing: 0.472
real num power timing: 3.352
int num X*X timing: 0.222
int num power timing: 3.739
In every case, sequence or straight numbers, X*X won by much
more of a margin than I think would be appropriate.
Perhaps there is a problem with power()????
If not, can anyone see a problem with the benchmarking???
yours truly (puzzled) --Hawke'
2. Re: POWER()
Hawke writes:
> It seems that power(x,2) is not quite right as far as speed.
> It says in library.doc that powers of 2 are calculated very
> efficiently.
By "power of 2", I meant power(2,n) where n is a fairly small
positive integer (from 0 to 29).
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/
3. Re: POWER()
>It seems that power(x,2) is not quite right as far as speed.
>It says in library.doc that powers of 2 are calculated very
>efficiently.
Didn't they mean power(2,x) ? :)
Tom
4. Re: POWER()
>>It seems that power(x,2) is not quite right as far as speed.
>>It says in library.doc that powers of 2 are calculated very
>>efficiently.
>Didn't they mean power(2,x) ? :)
No, the example they gave was x*x
And RDS has optimized the 2^n type of expressions, prolly using
asm-shifting.
No calculation involved.
However, x*x can never be faster than power (x, 2) .. the preproccesor could
optimize for this ...
Ralf Nieuwenhuijsen
nieuwen at xs4all.nl
5. Re: POWER()
- Posted by Hawke <mdeland at NWINFO.NET>
Sep 19, 1998
-
Last edited Sep 20, 1998
Ralf Nieuwenhuijsen wrote:
> However, x*x can never be faster than power (x, 2)
ummmmmm......
that's what i've been trying to point out....
x*x is much much much faster according to every
benchmark ive thought of... (more than the ones
i posted)... by a very large, very disturbing margin...
6. Re: POWER()
- Posted by Hawke <mdeland at NWINFO.NET>
Sep 19, 1998
-
Last edited Sep 20, 1998
Robert Craig wrote:
> Hawke writes:
>> It says in library.doc that powers of 2 are calculated very
>> efficiently.
> By "power of 2", I meant power(2,n) where n is a fairly small
> positive integer (from 0 to 29).
DOH! i knew dat :)
okay... so i didnt... ;)
>> It seems that power(x,2) is not quite right as far as speed.
this is the part that i'm actually worried about...
when x*x returns the same value (int,atom,sequence) as
power(x,2) between 3 and 30 times faster...
ummmm sumfin wrong dontcha think?
i could understand like 29.2 seconds versus 29.7 seconds,
or sumfin like that... but...
squares are real common, you know?
dare we try benchmarking cubes???
(not quite as common, but still used a lot in certain
formulas: cubic footage/volume, & air flow for starters)
i'm not complaining mind you... it's just a dilemma that
i happened to notice and thought should be pointed out...
I would, however, like to make a formal request for
an addition to Euphoria syntax.
It meets the following requirements:
1>Does it improve or uphold readability/clean coding? yes.
2>Does it enhance Euphoria, perhaps giving it
compliance with known standards/conventions? yes.
3>Would it be used? most assuredly.
4>Would it slow Euphoria down? no, in fact, it may
speed certain things up, as it will reduce function
calls for a commonly used activity.
5>Would it be difficult to implement? shouldn't be...
6>Would it represent a "hack"? no.
7>Would it hinder maintainability? no.
This request is simply the addition of the caret "^"
to the list of mathmatics operators for indicating
an object is to be raised to a power of.
The caveat is that objects on the righthand might
be less than 1.
25^0.5
{3,4,5}^{2,1/2,-2}
well, those examples get a little wierd...
I would be happy if, at the point this suggestion was
implemented, only values/objects greater than 1
were allowed on the righthand side...
at least until we all discuss this and determine
what we want done with objects <1 on the right...
If we restricted the righthand side to only integers
1 or higher, then very fast optimizations could be made
for the "^" and power() used for the 'tricky' cases...
I would also be happy, ecstatic in fact, if the
^ simply replaced power(), especially if it
reduced any function calling overhead.
--Hawke'
7. Re: POWER()
Hawke writes:
> when x*x returns the same value (int,atom,sequence) as
> power(x,2) between 3 and 30 times faster...
> ummmm sumfin wrong dontcha think?
The Euphoria power() function normally
calls the C pow() function which is very general
but very slow. It can handle x to the power of y
for just about any x and y, e.g. 7.88 to the power -2.341
To speed things up, Euphoria detects at execution-time
a couple of special cases such as powers of 2 (which
becomes right-shifts), and small integers raised to small
powers (which become integer multiplies).
Currently, power(x,2) is not optimized when x
is a floating-point number, or a fairly large integer.
x*x is much faster as you have noticed.
For the next release, I'll look into automatically
converting all occurrences of power(x,2) into x*x.
I'll detect this case at compile-time, so no
extra overhead will be added to power().
x could be a simple variable or a complicated
expression. It will be evaluated once, and then
the result will be multiplied by itself.
Usually 2 will be hard-coded into the power()
call. People rarely make the power, i.e. 2, a
variable or expression when they want to square
something.
Declaring a constant with the value 2 will also
work. e.g.
constant MYPOWER = 2
This is an example of where declaring
things as "constant" can make your program run faster,
since Euphoria always knows at compile-time what the
value of a constant is.
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/
8. Re: POWER()
Rob Craig wrote:
> For the next release, I'll look into automatically
> converting all occurrences of power(x,2) into x*x.
I forgot to mention that this conversion will also work when x is
a sequence (of any complexity).
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/
9. Re: POWER()
- Posted by Hawke <mdeland at NWINFO.NET>
Sep 20, 1998
-
Last edited Sep 21, 1998
Robert Craig wrote:
>> Hawke writes:
>> when x*x returns the same value (int,atom,sequence) as
>> power(x,2) between 3 and 30 times faster...
>> ummmm sumfin wrong dontcha think?
>For the next release, I'll look into automatically
>converting all occurrences of power(x,2) into x*x.
thank you.
thank you for listening, and thank you for doing.
those are the 2 hardest things, listening and doing...
--Hawke'