[Haskell-cafe] Efficient Factoring Out of Constants

Phil pbeadling at mail2web.com
Sat Jan 17 18:00:43 EST 2009


On 17/01/2009 20:45, "Eugene Kirpichov" <ekirpichov at gmail.com> wrote:

> A very short time ago Simon Marlow (if I recall correctly) commented
> on this topic: he told that this transformation usually improves
> efficiency pretty much, but sometimes it leads to some problems and it
> shouldn't be done by the compiler automatically. Search the recent
> messages for 'map' and one of them will have an answer :)
> 

Thanks for the tip - I'll have a look for this.

I've also tried a third test using the two possibilities below, both have
separate structures for variable and constant data, but the second example
explicitly factors out passing this around as in previous examples.  Here
there is no discernable difference between the two methods' timed results,
if anything the first one which is not explicitly factored out had a slight
edge in tests.

Therefore it would seem that providing you separate the constant and
variable data out into the separate parameters, the compiler will do the
rest for you.  This suggests that the compiler is indeed smart; the
staticData is not copied from iteration to iteration providing the whole
structure is constant, but when you mix and match within a structure itself
the compiler isn't smart enough to factor out the constant members.  I
suppose a conclusive test would be to add a very large amount of constant
data to the structure to see if results were still similar.

Note: strictIdx is just a strict version of !!:

getAveragePayoff :: Double -> Double -> Int -> Word64 -> Double
getAveragePayoff startStock endTime iterations seedForSeed = average
  where
    staticData = MCStatic startStock endTime
    average = ( runningSum $ snd $ strictIdx
                iterations ( iterate mcSimulate (staticData, (MCState
seedForSeed 0)) ) )
              / fromIntegral iterations

_______

getAveragePayoff :: Double -> Double -> Int -> Word64 -> Double
getAveragePayoff startStock endTime iterations seedForSeed = average
  where
    staticData = MCStatic startStock endTime
    average = ( runningSum  $ strictIdx
                iterations ( iterate mcWrapper (MCState seedForSeed 0) ) )
              / fromIntegral iterations
      where
        mcWrapper = \stateData -> snd $ mcSimulate (staticData, stateData)




On 17/01/2009 20:45, "Eugene Kirpichov" <ekirpichov at gmail.com> wrote:

> A very short time ago Simon Marlow (if I recall correctly) commented
> on this topic: he told that this transformation usually improves
> efficiency pretty much, but sometimes it leads to some problems and it
> shouldn't be done by the compiler automatically. Search the recent
> messages for 'map' and one of them will have an answer :)
> 
> 



More information about the Haskell-Cafe mailing list