我有一个应用程序花费大约80%的时间使用Kahan求和算法计算大型列表(10 ^ 7)的高维向量(dim = 100)的质心.我已经尽力优化求和,但它仍然比同等的C实现慢20倍.分析表明罪犯是unsafeRead和unsafeWrite来自的功能Data.Vector.Unboxed.Mutable.我的问题是:这些功能真的很慢还是我误解了性能分析统计数据?
这是两个实现.使用llvm后端使用ghc-7.0.3编译Haskell.C one用llvm-gcc编译.
哈斯克尔的卡汉总结:
{-# LANGUAGE BangPatterns #-}
module Test where
import Control.Monad ( mapM_ )
import Data.Vector.Unboxed ( Vector, Unbox )
import Data.Vector.Unboxed.Mutable ( MVector )
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import Data.Word ( Word )
import Data.Bits ( shiftL, shiftR, xor )
prng :: Word -> Word
prng w = w' where
!w1 = w `xor` (w `shiftL` 13)
!w2 = …Run Code Online (Sandbox Code Playgroud)