[Haskell-cafe] Profiling nested case

Mitar mmitar at gmail.com
Fri Jul 11 19:46:58 EDT 2008


Hi!

This is not all. If I compare performance of those two semantically
same functions:

castRayScene1 :: Ray -> ViewportDotColor
castRayScene1 (Ray vd o d) = ViewportDotColor vd (castRay' noColor 0)
  where castRay' color@(VoxelColor _ _ _ alpha) depth | depth >
depthOfField = color
                                                      | alpha < 0.001
      = castRay' pointColor (depth + distance')
                                                      | alpha > 0.999
      = color
                                                      | otherwise
      = castRay' newColor (depth + distance')
          where (# pointColor, distance #) = worldScene (o <+> (d <* depth))
                distance'                  = max 1 distance
                newColor                   = addColor color pointColor

and:

castRay :: World -> Ray -> ViewportDotColor
castRay world (Ray vd o d) = ViewportDotColor vd (castRay' noColor 0)
  where castRay' color@(VoxelColor _ _ _ alpha) depth | depth >
depthOfField = color
                                                      | alpha < 0.001
      = castRay' pointColor (depth + distance')
                                                      | alpha > 0.999
      = color
                                                      | otherwise
      = castRay' newColor (depth + distance')
          where (# pointColor, distance #) = world (o <+> (d <* depth))
                distance'                  = max 1 distance
                newColor                   = addColor color pointColor

castRayScene2 :: Ray -> ViewportDotColor
castRayScene2 = castRay worldScene

is the program which uses castRayScene1 1.35 faster then the program
which uses castRayScene2 (37 seconds versus 50 seconds).

(Compiler with GHC 6.8.3 and -O2 switch. Program is executing almost
just this function over and over again.)

It is somehow award that passing function as an argument slow down the
program so much. Is not Haskell a functional language and this such
(functional) code reuse is one of its main points?

Of course. I could use some preprocessor/template engine to
change/find/replace castRay-like function into a castRayScene1 before
compiling. But this somehow kills the idea of a compiler? Smart
compiler. Which should do things for you?

The same as my previous example. Where a hard-coded list was not
optimized. (Like it would change during the execution.) It looks like
my program would be interpreted and not compiled.


Mitar


More information about the Haskell-Cafe mailing list