Re: POWER()
- Posted by Robert Craig <rds at EMAIL.MSN.COM> Sep 20, 1998
- 597 views
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/