Skip to content

Instantly share code, notes, and snippets.

@kfl
Created May 3, 2012 19:27
Show Gist options
  • Save kfl/2588483 to your computer and use it in GitHub Desktop.
Save kfl/2588483 to your computer and use it in GitHub Desktop.
Benchmark template for TiPL 2012, for testing map implementations
-- Benchmark template for TiPL 2012, for testing map implementations
-- Compile with the command: ghc -O3 -W --make mapbench.hs -o mapbench
import qualified Criterion.Main as C
isPrime :: Integral a => a -> Bool
isPrime n = null [factor | factor <- [2..floor $ sqrt $ fromIntegral n]
, n `mod` factor == 0]
numbers = [2,3..]
kmap f = foldr (\x res -> f x : res) []
main = do
let sizes = [ 10000, 100000, 1000000]
inputs = [(n, take n numbers) | n <- sizes]
benchmarks = [ C.bench (name ++ show n) $ C.nf mapprime ns
| (n, ns) <- inputs,
(name, mapprime) <- [("Data.List.map ", map$isPrime),
("kmap " , kmap$isPrime)]]
C.defaultMain benchmarks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment