BenchmarkUArray.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE CPP #-}

module Main where

import UArrays
#ifdef UNBOXED
import Data.Array.Unboxed
#else
import Data.Array
#endif

data Color = Red | Yellow | Green | Blue
           deriving (Show, Ord, Enum, Bounded, Eq)

shift :: (Eq a, Ord a, Bounded a, Enum a) => a -> a
shift x | x == maxBound = minBound
        | otherwise     = succ x

size = 10000000

#ifdef UNBOXED
testArray :: UArray Int Color
#else
testArray :: Array Int Color
#endif
testArray = listArray (0, size) $ cycle [Red, Yellow, Green]

#ifdef UNBOXED
#define ELEMENT (size - 48849)
#else
#define ELEMENT (size - 48848)
#endif

benchmark :: IO ()
benchmark =
  print $ (testArray // [(i, shift (testArray ! i)) | i <- [0..(size-1)]]) ! ELEMENT

main = benchmark