/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this Haskell GHC program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
100.620.581,916658  8% 2% 2% 98%
116.946.601,916658  46% 3% 57% 4%
1287.4684.031,932658  100% 2% 2% 2%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read fannkuch-redux benchmark to see what this program should do.

 notes

The Glorious Glasgow Haskell Compilation System, version 7.8.2

 fannkuch-redux Haskell GHC #4 program source code

{-  The Computer Language Benchmarks Game

    http://benchmarksgame.alioth.debian.org/

    contributed by Branimir Maksimovic
   optimized/rewritten by Bryan O'Sullivan
-}

import System.Environment
import Text.Printf
import Data.Bits

import qualified Data.Vector.Unboxed.Mutable as VM
import qualified Data.Vector.Generic.Mutable as VG
import qualified Data.Vector.Unboxed as V

main = do
    n <- getArgs >>= readIO.head
    (checksum,maxflips) <- fannkuch n
    printf "%d\nPfannkuchen(%d) = %d\n" checksum n maxflips

fannkuch :: Int -> IO (Int, Int)
fannkuch n = do
    perm <- V.unsafeThaw $ V.enumFromN 1 n
    !tperm <- VG.new n
    !cnt <- VG.replicate n 0
    let 
        loop :: Int -> Int -> Int -> IO(Int,Int)
        loop !c !m !pc = do
        b <- next_permutation perm n cnt
        if b == False
            then return (c,m)
            else do
            VM.unsafeCopy tperm perm
            let count_flips !flips = {-# SCC "count_flips" #-} do
                f <- VM.unsafeRead tperm 0
                if f == 1
                then loop (c + (if pc .&. 1 == 0 then flips else -flips))
                            (max m flips)
                            (pc+1)
                else do
                    VG.reverse $ VM.unsafeSlice 0 f tperm
                    count_flips (flips+1)
            count_flips 0
    loop 0 0 1

next_permutation :: VM.IOVector Int -> Int -> VM.IOVector Int -> IO Bool
next_permutation perm !n !cnt = loop 1
    where
    loop :: Int -> IO Bool
    loop i
        | i >= n = done i
        | otherwise = do
            tmp <- VM.unsafeRead perm 0
            let 
                rotate :: Int -> IO()
                rotate j
                    | j >= i = VM.unsafeWrite perm i tmp
                    | otherwise = do
                        !v <- VM.unsafeRead perm (j+1)
                        VM.unsafeWrite perm j v
                        rotate (j+1)
            rotate 0
            v <- VM.unsafeRead cnt i
            if v >= i
            then VM.unsafeWrite cnt i 0 >> loop (i+1)
            else done i
    done i
        | i >= n = return False
        | otherwise = do
            v <- VM.unsafeRead cnt i
            VM.unsafeWrite cnt i (v+1)
            return True

 make, command-line, and program output logs

Wed, 16 Apr 2014 23:39:03 GMT

MAKE:
mv fannkuchredux.ghc-4.ghc fannkuchredux.ghc-4.hs
/usr/local/src/ghc-7.8.2/bin/ghc --make -fllvm -O2 -XBangPatterns -threaded -rtsopts  fannkuchredux.ghc-4.hs -o fannkuchredux.ghc-4.ghc_run
[1 of 1] Compiling Main             ( fannkuchredux.ghc-4.hs, fannkuchredux.ghc-4.o )
Linking fannkuchredux.ghc-4.ghc_run ...
rm fannkuchredux.ghc-4.hs
1.66s to complete and log all make actions

COMMAND LINE:
./fannkuchredux.ghc-4.ghc_run +RTS -N4 -RTS 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65

Revised BSD license

  Home   Conclusions   License   Play