[Haskell-beginners] List operations

Ertugrul Soeylemez es at ertes.de
Thu May 19 18:15:31 CEST 2011


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

> However, since you're asking about cost, which indicates that you care
> for performance, the above would be better written as
>
> [x*x*x | x <- list]
>
> unless you depend on the small differences in the outcome [I'm not
> quite sure how many bits may be affected, not many, typically none or
> one].  Functions like (**), exp, log, sin, cos, ... are slow, very
> slow.  If the exponent is a small (positive) integer, specifically
> giving a sequence of multiplication steps is much faster, also using
> (^) instead of (**) is faster for small exponents (but slower than an
> explicit multiplication sequence).

Neither does this really match my intuition, 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:

    iterate (** 3) 1.000000001 !! 1000000
    iterate (^3) 1.000000001 !! 1000000
    iterate (\x -> x*x*x) 1.000000001 !! 1000000

Note that exponentiation is a cheap operation on Double.


Greets
Ertugrul


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





More information about the Beginners mailing list