[Haskell-beginners] List operations

Ertugrul Soeylemez es at ertes.de
Fri May 20 01:34:59 CEST 2011


Daniel Fischer <daniel.is.fischer at googlemail.com> wrote:

> > Neither does this really match my intuition,
>
> A multiplication takes ~1 clock cycle, calling out to pow takes way
> more (I confess, I don't know how many clock cycles a function call
> takes).
>
> > nor can I confirm it with an experiment.  Applying (** 3) a million
> > times to a Double takes a second and gives me the expected Infinity.
> > Applying (^3) or (\x -> x*x*x) a million times to the same value,
> > well, I didn't want to wait for it to finish.
> >
> > The experiment was ran with the following codes in GHCi:
>
> Don't benchmark in ghci, use compiled code.

You were right, a benchmark with optimized, compiled code gave me
different results.  I now tested with the following function:

    calc :: (Double -> Double) -> Double
    calc f = calc' 10000000 (1 - encodeFloat 1 (-53))
        where
        calc' :: Int -> Double -> Double
        calc' 0 x = x
        calc' n' x =
            let y = f x
                n = pred n'
            in y `seq` n `seq` calc' n y

Running this function with (\x -> x*x*x) was the fastest, followed by
(** 3) and then (^3).  In conclusion, multiplication is faster than
exponentiation for the special case of small exponents.  Since the speed
of (**) doesn't depend on the exponent (for Double), starting with an
exponent of 15 (**) outperformed the other two variants.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





More information about the Beginners mailing list