我制作了小型C模块以提高性能,但GHC并没有内联外部功能,而且调用成本消除了加速.例如,test.h:
int inc (int x);
Run Code Online (Sandbox Code Playgroud)
test.c:
#include "test.h"
int inc(int x) {return x + 1;}
Run Code Online (Sandbox Code Playgroud)
Test.hc:
{-# LANGUAGE ForeignFunctionInterface #-}
module Test (inc) where
import Foreign
import Foreign.C
foreign import ccall unsafe "test.h inc" c_inc :: CInt -> CInt
inc = fromIntegral . c_inc . fromIntegral
{-# INLINE c_inc #-}
{-# INLINE inc #-}
Run Code Online (Sandbox Code Playgroud)
Main.hs:
import System.Environment
import Test
main = do {args <- getArgs; putStrLn . show . inc . read . head $ …Run Code Online (Sandbox Code Playgroud) 虽然我有一个很好的LSFR C实现,我想我会在Haskell中尝试相同 - 只是为了看看它是怎么回事.到目前为止,我想出的是比C实现慢两个数量级,这引出了一个问题:性能如何得到改善?显而易见,这个小小的操作是瓶颈,而分析器确认了这一点.
这是使用列表的基线Haskell代码,并且Data.Bits:
import Control.Monad (when)
import Data.Bits (Bits, shift, testBit, xor, (.&.), (.|.))
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
tap :: [[Int]]
tap = [
[], [], [], [3, 2],
[4, 3], [5, 3], [6, 5], [7, 6],
[8, 6, 5, 4], [9, 5], [10, 7], [11, 9],
[12, 6, 4, 1], [13, 4, 3, 1], [14, 5, 3, 1], [15, 14],
[16,15,13,4], [17, 14], [18, 11], [19, 6, 2, 1],
[20, …Run Code Online (Sandbox Code Playgroud)