{-# LANGUAGE FlexibleInstances #-}
module Main where
import Criterion.Main
import Control.Exception (evaluate)
instance Benchmarkable (b -> a) where
run f n
| n <= 0 = return ()
| otherwise = evaluate f >> run f (n-1)
numOccur :: (Num b) => b -> [b] -> b
numOccur elem =
foldr (\x acc -> if x == elem then (acc+1) else acc) 0
playerMostOccur [a] = a
playerMostOccur (x:xs)
| numOccur x (x:xs) > numOccur (playerMostOccur xs) xs = x
| otherwise = playerMostOccur xs
playerMostOccur' [a] = a
playerMostOccur' (x:xs)
| numOccur x (x:xs) > numOccur pmo xs = x
| otherwise = pmo
where pmo = playerMostOccur' xs
somerepeat :: [Int]
somerepeat = [ (mod x 10) | x <- [0..1000] ]
main = defaultMain
[ bench "playerMostOccur" $ \n -> playerMostOccur somerepeat
, bench "playerMostOccur'" $ \n -> playerMostOccur' somerepeat
, bench "playerMostOccur" $ \n -> playerMostOccur [0..100]
, bench "playerMostOccur'" $ \n -> playerMostOccur' [0..100]
, bench "playerMostOccur" $ \n -> playerMostOccur [0..1000]
, bench "playerMostOccur'" $ \n -> playerMostOccur' [0..1000]
, bench "playerMostOccur" $ \n -> playerMostOccur [0..10000]
, bench "playerMostOccur'" $ \n -> playerMostOccur' [0..10000]
]