HI,

This is not real benchmark. I have a function called inside two getCurrentTime.
I had a request to handle over 100 comparisons per second, and couldn't achieve that with monadic version because it was too slow.
(I do not need the solution for the problem, i.e. how to speed it up. I moved the function out of IO and it is fine now.)

I must admit that I didn't have the slightest idea that the performance could be so different (a million times).
I'm not a Haskell expert and I just want to know what is eating time, so I do not do that in my own functions, or at least to be aware of the possible problem.


"...any of the two taking advantage of computations previously performed..."
I load both images and than run functions in both orders, and the results were the same.


BTW, this was the function:

diffA :: Image PixelRGB8 -> Image PixelRGB8 -> Image PixelRGBA8
diffA i1@(Image { imageWidth = w, imageHeight = h }) i2 = generateImage (pixGenA i1 i2) w h

pixGenA :: Image PixelRGB8 -> Image PixelRGB8 -> Int -> Int -> PixelRGBA8
pixGenA i1 i2 x y =
  if p1 == p2
    then PixelRGBA8 0 0 0 0
    else  PixelRGBA8 r g b 254
  where
    p1@(PixelRGB8 r g b) = pixelAt i1 x y
    p2                               = pixelAt i2 x y


The other is exactly the same, but has "withImage" instead of "generateImage". (and pixGenM is monadic)

diff2 :: Image PixelRGB8 -> Image PixelRGB8 -> IO (Image PixelRGBA8)
diff2 i1@(Image { imageWidth = w, imageHeight = h }) i2 = withImage w h (pixGenM i1 i2)

pixGenM :: Monad m => Image PixelRGB8 -> Image PixelRGB8 -> Int -> Int -> m PixelRGB8
...


Not much wisdom in it. :-)


vlatko


-------- Original Message --------
Subject: Re: [Haskell-cafe] JuicyFruit - explanation of speed difference of pure and monadic image generation
From: Alp Mestanogullari <alpmestan@gmail.com>
To: Joey Adams <joeyadams3.14159@gmail.com>
Cc: The Haskell Cafe <haskell-cafe@haskell.org>, vlatko.basic@gmail.com
Date: 20.03.2014 15:43


Could it be because you are calling withImage in IO whereas generateImage coes through ST? A lot of the nice performance numbers of JuicyPixels come from its carefully tailored ST usage, which in turn comes from theefficiency of unboxed mutable vectors (as in the "vector" package).

So could you post the benchmark result for a version where you runST on the result of withImage? That should be a fairer comparison. Also, writing a criterion benchmark would help and make sure the functions are run properly without any of the two taking advantage of computations previsouly performed by the other.