maybe vs cont vs id benchmark

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Criterion.Main                                          
                                                               
unsafeDiv x 0 = error "division by zero"                       
unsafeDiv x y = x `div` y                                      
                                                               
safeDiv x 0 = Nothing                                          
safeDiv x y = Just (x `div` y)                                 
                                                               
test1 :: Int -> [Int]                                          
test1 n = map (n `unsafeDiv`) [n,n-1..1]                       
                                                               
test2 :: Int -> Maybe [Int]                                    
test2 n = mapM (n `safeDiv`) [n-1,n-2..0]                      
                                                               
test3 :: Int -> Maybe [Int]                                    
test3 n = test3' Just [n-1,n-2..0]                             
  where test3' k []     = k []                                 
        test3' k (0:ns) = Nothing                              
        test3' k (n:ns) = test3' (k . (n:)) ns                 
                                                               
main = defaultMain                                             
  [ bench "test1" (nf test1 100000)                            
  , bench "test2" (nf test2 100000)                            
  , bench "test3" (nf test2 100000)                            
  ] 

maybe vs cont vs id benchmark (annotation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Criterion.Main                                          
                                                               
unsafeDiv x 0 = error "division by zero"                       
unsafeDiv x y = x `div` y                                      
                                                               
safeDiv x 0 = Nothing                                          
safeDiv x y = Just (x `div` y)                                 
                                                               
test1 :: Int -> [Int]                                          
test1 n = map (n `unsafeDiv`) [n,n-1..1]                       
                                                               
test2 :: Int -> Maybe [Int]                                    
test2 n = mapM (n `safeDiv`) [n-1,n-2..0]                      
                                                               
test3 :: Int -> Maybe [Int]                                    
test3 n = test3' Just [n-1,n-2..0]                             
  where test3' k []     = k []                                 
        test3' k (0:ns) = Nothing                              
        test3' k (n:ns) = test3' (k . (n:)) ns                 
                                                               
main = defaultMain                                             
  [ bench "test1" (nf test1 100000)                            
  , bench "test2" (nf test2 100000)                            
  , bench "test3" (nf test3 100000)                            
  ]