I am trying to learn / evaluate Haskell and I am afraid to get an efficient executable for a simple case. The test I'm using is a PRNG sequence (PCG32 RNG replication). I wrote this as an iteration of the basic state transition function (now I only look at the state).
{-# LANGUAGE BangPatterns #-}
import System.Environment (getArgs)
import Data.Bits
import Data.Word
iterate' f !x = x : iterate' f (f x)
main = print $ pcg32_rng 100000000
pcg32_random_r :: Word64 -> Word64 -> Word64
pcg32_random_r !i !state = state * (6364136223846793005 :: Word64) + (i .|. 1)
{-# INLINE pcg32_random_r #-}
pcg32_rng_s = iterate' (pcg32_random_r 1) 0
pcg32_rng n = pcg32_rng_s !! (n - 1)
I can get this code to compile and run. It still uses a lot more memory than it should, and runs 10 times slower than the C equivalent. The main problem is that the iteration does not turn into a simple loop.
What am I missing to get GHC to create faster / more efficient code here?
EDIT
C, , , , , . , , - .
#include <stdio.h>
#include <stdint.h>
int main() {
uint64_t oldstate,state;
int i;
for(i=0;i<100000000;i++) {
oldstate = state;
state = oldstate * 6364136223846793005ULL + (1|1);
}
printf("%ld\n",state);
}
initly Prelude iterate
, . "" .
, GHC inline pcg32_random_r
, , , , . GHC, .
@WillemVanOnsem , perform
C, pcg32_random_r
. Haskell GHC. , perform
, ?
, ?
, , , ( /,...), , , Haskell .