Haskell 优化和、ZipList、向量和未装箱类型

Haskell 优化和、ZipList、向量和未装箱类型,haskell,optimization,lazy-evaluation,Haskell,Optimization,Lazy Evaluation,我已经确定了以下热点函数,它目前占我程序执行时间的25%: type EncodeLookup = [(GF256Elm, [GF256Elm])] -- | Given the coefficients of a Polynomial and an x value, -- calculate its corresponding y value. -- coefficients are in ascending order [d, a1, a2, a3...] where y = d + a1

我已经确定了以下热点函数,它目前占我程序执行时间的25%:

type EncodeLookup = [(GF256Elm, [GF256Elm])]

-- | Given the coefficients of a Polynomial and an x value,
-- calculate its corresponding y value.
-- coefficients are in ascending order [d, a1, a2, a3...] where y = d + a1*x + a2*x^2 ...
calc :: EncodeLookup -> [GF256Elm] -> [GF256Elm]
calc xsLookup poly =
    map f xsLookup
        where
            f (_, pList) = 
                let zl = (*) <$> ZipList (pList)  <*> ZipList poly
                    lst = getZipList zl
                    s = sum lst 
                in s
我的意见:

  • sum
    的缓慢可能来自懒惰的列表thunks
  • *
    计算也需要一段时间。您可以看到
    GF256Elm
    的完整实现<代码>*基本上是预生成表的向量查找和一些位翻转
  • “ZipList”似乎也要花费大量的时间
  • 我的问题是:

  • 我将如何优化此函数?特别是关于
    sum
    ——在列表中使用
    deepSeq
    会更快吗

  • 我是否应该为
    GF256Elm
    使用未装箱的
    Int#
    类型?还有什么其他方法可以提高
    GF256Elm
    的操作速度


  • 谢谢

    您是否尝试过将
    sum
    更改为
    foldl'(+)0
    为什么要使用ZipList?这是一种超精密的机器。试着摆脱你的第三大成本中心。奖金:把这个贴在总数上应该会熔断。@Thomas,不知道那个!谢谢@托马斯:看来你可以把这句话变成一个答案,因为它解决了问题?@jatcwang,这真的解决了问题吗?
                                        individual     inherited
    COST CENTRE       no.     entries  %time %alloc   %time %alloc
    calc              544      794418    1.6    3.1    27.5   19.7
     calc.f           698     3972090    0.9    0.0    25.9   16.6
      calc.f.s        709     3972090    7.4    6.2    11.0    7.8
       fromInteger    711     3972090    0.7    0.0     0.7    0.0
       +              710    11916270    2.9    1.6     2.9    1.6
      calc.f.lst      708     3972090    0.0    0.0     0.0    0.0
      calc.f.zl       699     3972090    6.8    8.8    14.0    8.8
       *              712    11916270    7.2    0.0     7.2    0.0