| 1 | {-# OPTIONS_GHC -fglasgow-exts #-} |
|---|
| 2 | |
|---|
| 3 | module Main (main) where |
|---|
| 4 | |
|---|
| 5 | import Prelude hiding (reverse) |
|---|
| 6 | |
|---|
| 7 | import Control.Monad |
|---|
| 8 | |
|---|
| 9 | import GHC.Base |
|---|
| 10 | import GHC.IOBase |
|---|
| 11 | import GHC.Ptr |
|---|
| 12 | |
|---|
| 13 | import Foreign |
|---|
| 14 | |
|---|
| 15 | import System.Environment |
|---|
| 16 | |
|---|
| 17 | reverse :: Addr# -> Int# -> Int# -> State# RealWorld -> State# RealWorld |
|---|
| 18 | reverse a i j s |
|---|
| 19 | | i <# j = case readIntOffAddr# a i s of { (# s, x #) -> |
|---|
| 20 | case readIntOffAddr# a j s of { (# s, y #) -> |
|---|
| 21 | case writeIntOffAddr# a j x s of { s -> |
|---|
| 22 | case writeIntOffAddr# a i y s of { s -> |
|---|
| 23 | reverse a (i +# 1#) (j -# 1#) s }}}} |
|---|
| 24 | | otherwise = s |
|---|
| 25 | |
|---|
| 26 | bench :: Int# -> Int# -> IO () |
|---|
| 27 | bench k n = do p@(Ptr a) <- mallocArray (I# n) :: IO (Ptr Int) |
|---|
| 28 | fill a n |
|---|
| 29 | IO (go a k) |
|---|
| 30 | free p |
|---|
| 31 | where |
|---|
| 32 | go a 0# s = (# s, () #) |
|---|
| 33 | go a i s = case reverse a 0# (n -# 1#) s of s -> go a (i -# 1#) s |
|---|
| 34 | |
|---|
| 35 | fill :: Addr# -> Int# -> IO () |
|---|
| 36 | fill a n = IO (go 0#) |
|---|
| 37 | where |
|---|
| 38 | go i s |
|---|
| 39 | | i <# n = case writeIntOffAddr# a i i s of s -> go (i +# 1#) s |
|---|
| 40 | | otherwise = (# s, () #) |
|---|
| 41 | |
|---|
| 42 | main = do (I# k:I# n:_) <- map read `fmap` getArgs |
|---|
| 43 | bench k n |
|---|
| 44 | putStrLn "Done." |
|---|