Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created February 12, 2014 11:15
Show Gist options
  • Select an option

  • Save pasberth/8953682 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/8953682 to your computer and use it in GitHub Desktop.
$ ghc -O2 a.hs
$ time ./a
./a  1.39s user 1.70s system 69% cpu 4.479 total
$ ghc -O2 b.hs
$ time ./b
./b  1.52s user 1.73s system 73% cpu 4.448 total
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ExplicitForAll #-}
import Prelude hiding (length)
import Control.Monad
incr :: Int -> Int
{-# INLINE incr #-}
incr = (+1)
lengthCPS :: forall a. (Int -> Int) -> [a] -> Int
lengthCPS cont [] = cont 0
lengthCPS cont (_:xs) = lengthCPS (cont . incr) xs
length :: forall a. [a] -> Int
length = lengthCPS id
main :: IO ()
main = do
forM_ [0..1000000] $ const $ do
print $ length [0..10000]
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ExplicitForAll #-}
import Prelude hiding (length)
import Control.Monad
length :: forall a. [a] -> Int
length [] = 0
length (_:xs) = 1 + length xs
main :: IO ()
main = do
forM_ [0..1000000] $ const $ do
print $ length [0..10000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment