Skip to content

Instantly share code, notes, and snippets.

@UnkindPartition
Created January 11, 2017 10:19
Show Gist options
  • Save UnkindPartition/9343ac79e2697edcc28fdfee858fadfe to your computer and use it in GitHub Desktop.
Save UnkindPartition/9343ac79e2697edcc28fdfee858fadfe to your computer and use it in GitHub Desktop.
forM_ speed test
benchmarking vec base
time 15.54 μs (15.43 μs .. 15.63 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 15.45 μs (15.40 μs .. 15.51 μs)
std dev 180.4 ns (157.0 ns .. 209.1 ns)
benchmarking vec roman
time 16.86 μs (16.77 μs .. 16.95 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 16.86 μs (16.81 μs .. 16.92 μs)
std dev 191.8 ns (170.7 ns .. 219.3 ns)
benchmarking vec michael
time 16.20 μs (16.08 μs .. 16.31 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 16.15 μs (16.08 μs .. 16.23 μs)
std dev 254.4 ns (208.1 ns .. 318.7 ns)
variance introduced by outliers: 12% (moderately inflated)
benchmarking set base
time 23.82 μs (23.71 μs .. 23.95 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 23.81 μs (23.73 μs .. 23.94 μs)
std dev 356.5 ns (266.4 ns .. 530.4 ns)
variance introduced by outliers: 11% (moderately inflated)
benchmarking set roman
time 17.76 μs (17.67 μs .. 17.85 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 17.75 μs (17.69 μs .. 17.81 μs)
std dev 205.8 ns (175.1 ns .. 239.6 ns)
benchmarking set michael
time 16.47 μs (16.40 μs .. 16.54 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 16.45 μs (16.41 μs .. 16.50 μs)
std dev 158.8 ns (126.6 ns .. 200.1 ns)
import Criterion
import Criterion.Main
import Data.Foldable
import qualified Data.Vector as V
import qualified Data.Set as Set
main = defaultMain
[ bench "vec base" $ whnfIO $ forM_ vec do_stuff
, bench "vec roman" $ whnfIO $ forM_1 vec do_stuff
, bench "vec michael" $ whnfIO $ forM_2 vec do_stuff
, bench "set base" $ whnfIO $ forM_ set do_stuff
, bench "set roman" $ whnfIO $ forM_1 set do_stuff
, bench "set michael" $ whnfIO $ forM_2 set do_stuff
]
forM_1 :: (Monad m, Foldable t) => t a -> (a -> m ()) -> m ()
forM_1 a f =
let l = toList a
b = (False <$ tail l) ++ [True]
in
foldr (\(x,b) r -> if b then f x else f x >> r)
(return ()) (zip l b)
forM_2 :: (Applicative m, Foldable f) => f a -> (a -> m ()) -> m ()
forM_2 a f =
go (toList a)
where
go [] = pure ()
go [x] = f x -- here's the magic
go (x:xs) = f x *> go xs
{-# NOINLINE do_stuff #-}
do_stuff _ = return ()
{-# NOINLINE vec #-}
vec :: V.Vector Int
vec = V.fromList [1..10^3]
{-# NOINLINE set #-}
set :: Set.Set Int
set = Set.fromList [1..10^3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment