Comparison of lazy and strict evaluations in Haskell

邮差的信 提交于 2019-12-12 19:16:51

问题


I implemented the Winograd algorithm on Haskell and tried to speed up the algorithm due to strict calculations. In this I succeeded, but I completely did not understand why, adding strictness, it starts to work faster. Since my code for this algorithm is large enough, I wrote two small functions that demonstrate this problem.

module Main where
import qualified Data.Vector as V
import qualified Data.Matrix as M

import Control.DeepSeq
import Control.Exception
import System.Clock
import Data.Time

matrixCtor x y size = M.matrix size size $ \(i,j) -> x*i+y*j

group v s = foldl (\acc i ->acc + V.unsafeIndex v i * V.unsafeIndex v (i+1)) 0 [0,2..s-1]

size = 3000 :: Int

testWithForce :: IO ()
testWithForce = do
  let a = matrixCtor 2 1 size
  evaluate $ force a

  start <- getCurrentTime

  let c = V.generate size $ \j -> M.getCol (j+1) a
  evaluate $ force c
  let d = foldl (\acc i ->acc + group (V.unsafeIndex c i) size) 0 [0,1..(size-1)]
  evaluate $ force d

  end <- getCurrentTime
  print (diffUTCTime end start)


testWithoutForce :: IO ()
testWithoutForce = do
  let a = matrixCtor (-2) 1 size
  evaluate $ force a

  start <- getCurrentTime

  let c = V.generate size $ \j -> M.getCol (j+1) a
  let d = foldl (\acc i ->acc + group (V.unsafeIndex c i) size) 0 [0,1..(size-1)]
  evaluate $ force d

  end <- getCurrentTime
  print (diffUTCTime end start)

main :: IO ()
main = do
  testWithForce
  testWithoutForce

In the implementation of the algorithm, the matrices are computed before use, just like here. In the function testWithForce I calculate the value c before it is used. In this case, the function testWithForce works faster than the function testWithoutForce. I got the following results:

0.945078s --testWithForce
1.785158s --testWithoutForce

I just can not understand why strictness in this case speeds up the work so much.


回答1:


Pardon the non-answer, but make sure to control for GC: it appears that the second function may be burdened with the GC from the previous one, thereby inflating the difference.

I can reproduce what you're seeing:

$ ghc -O3 --make foo.hs && ./foo
[1 of 1] Compiling Main             ( foo.hs, foo.o )
Linking foo ...
1.471109207s
2.001165795s

However, when I flipped the order of the test, the result was different:

main = do
  testWithoutForce 
  testWithForce

$  ghc -O3 --make foo.hs && ./foo
1.626452918s
1.609818958s

So I made main GC between each test:

import System.Mem

main = do
  performMajorGC
  testWithForce
  performMajorGC
  testWithoutForce

The forced one is still faster, but the difference was massively reduced:

1.460686986s
1.581715988s


来源:https://stackoverflow.com/questions/48714703/comparison-of-lazy-and-strict-evaluations-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!