Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created March 23, 2012 11:39
Show Gist options
  • Save yihuang/2169855 to your computer and use it in GitHub Desktop.
Save yihuang/2169855 to your computer and use it in GitHub Desktop.
project euler 14
import Criterion.Main
next :: Int -> Int
next n | even n = n `div` 2
| otherwise = 3*n+1
len :: Int -> Int
len = length . takeWhile (/=1) . iterate next
len' :: Int -> Int -> Int
len' k 1 = k
len' k n = len' (k+1) (next n)
main =
defaultMain
[ bgroup "default"
[ bench "iterate" $ whnf len 350
, bench "tail recur" $ whnf (len' 1) 350
]
]
{--
- warming up
- estimating clock resolution...
- mean is 4.375940 us (160001 iterations)
- found 1174 outliers among 159999 samples (0.7%)
- 1094 (0.7%) high severe
- estimating cost of a clock call...
- mean is 1.440690 us (31 iterations)
- found 3 outliers among 31 samples (9.7%)
- 3 (9.7%) high mild
-
- benchmarking default/iterate
- mean: 4.302455 us, lb 4.291602 us, ub 4.313241 us, ci 0.950
- std dev: 55.30098 ns, lb 47.49248 ns, ub 72.13509 ns, ci 0.950
- found 1 outliers among 100 samples (1.0%)
- variance introduced by outliers: 5.660%
- variance is slightly inflated by outliers
-
- benchmarking default/tail recur
- mean: 1.470772 us, lb 1.467614 us, ub 1.474461 us, ci 0.950
- std dev: 17.48218 ns, lb 15.19143 ns, ub 20.64320 ns, ci 0.950
--}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment