Skip to content

Instantly share code, notes, and snippets.

@kfl
Created August 19, 2010 07:22
Show Gist options
  • Save kfl/537292 to your computer and use it in GitHub Desktop.
Save kfl/537292 to your computer and use it in GitHub Desktop.
Benchmarking of build-in length and custom length function using criterion
{-
Benchmarking of build-in length and custom length function using criterion.
Compile with the command: ghc -O3 -W --make lengthbench.hs -o lengthbench
Results (timings in seconds, std. dev. negligible):
build-in length:
1000000 0.004
2000000 0.009
4000000 0.016
8000000 0.035
16000000 0.082
32000000 0.162
64000000 0.322
custom length (klength):
1000000 0.014
2000000 0.026
4000000 0.046
8000000 0.078
16000000 0.128
32000000 0.198
64000000 0.316
-}
import qualified Criterion.Main as C
import Data.List (foldl')
makeList :: Int -> [Int]
makeList n = take n ones
where ones = 1 : ones
klength :: [a] -> Int
klength ls = foldl' (\n _ -> n+1) 0 ls
main :: IO()
main = do
C.defaultMain
[ C.bench "length 1000000" $ C.whnf length (makeList 1000000)
, C.bench "length 2000000" $ C.whnf length (makeList 2000000)
, C.bench "length 4000000" $ C.whnf length (makeList 4000000)
, C.bench "length 8000000" $ C.whnf length (makeList 8000000)
, C.bench "length 16000000" $ C.whnf length (makeList 16000000)
, C.bench "length 32000000" $ C.whnf length (makeList 32000000)
, C.bench "length 64000000" $ C.whnf length (makeList 64000000)
, C.bench "klength 1000000" $ C.whnf klength (makeList 1000000)
, C.bench "klength 2000000" $ C.whnf klength (makeList 2000000)
, C.bench "klength 4000000" $ C.whnf klength (makeList 4000000)
, C.bench "klength 8000000" $ C.whnf klength (makeList 8000000)
, C.bench "klength 16000000" $ C.whnf klength (makeList 16000000)
, C.bench "klength 32000000" $ C.whnf klength (makeList 32000000)
, C.bench "klength 64000000" $ C.whnf klength (makeList 64000000)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment