Skip to content

Instantly share code, notes, and snippets.

@JakobBruenker
Last active December 2, 2020 07:02
Show Gist options
  • Save JakobBruenker/22a6ae3e562fa78242be4a30f14976d8 to your computer and use it in GitHub Desktop.
Save JakobBruenker/22a6ae3e562fa78242be4a30f14976d8 to your computer and use it in GitHub Desktop.
{-# LANGUAGE NumDecimals, BlockArguments #-}
import System.Environment
import Data.List
f 0 = (length .) . filter
f 1 = count
f 2 = count'
f 3 = count''
a = length $ filter odd [1..1e7]
b = count'' odd [1..1e7]
count p [] = 0
count p (x:xs) = (if p x then 1 else 0) + count p xs
count' p = foldr (\x acc -> if p x then acc + 1 else acc) 0
count'' p = foldl' (\acc x -> if p x then acc + 1 else acc) 0
main = do
[i] <- map read <$> getArgs
print . ($[1..1e8]) . ($odd) . f $ i
{-
$ ghc Fusion.hs -O2
(length .) . filter
$ time ./Fusion 0
50000000
real 0m3.052s
user 0m3.013s
sys 0m0.010s
count
$ time ./Fusion 1
50000000
real 0m3.888s
user 0m3.658s
sys 0m0.202s
count'
$ time ./Fusion 2
50000000
real 0m3.906s
user 0m3.654s
sys 0m0.222s
count''
$ time ./Fusion 3
50000000
real 0m3.059s
user 0m3.024s
sys 0m0.011s
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment