Skip to content

Instantly share code, notes, and snippets.

@amosr
Created April 24, 2017 01:04
Show Gist options
  • Save amosr/7d6f326371405d69dbf19187725f24b8 to your computer and use it in GitHub Desktop.
Save amosr/7d6f326371405d69dbf19187725f24b8 to your computer and use it in GitHub Desktop.
if bench
-- Which is faster?
-- Only difference is order of branches in 'if'.
sourceOfVector :: (Monad m, Generic.Vector v a) => v a -> Source m a
sourceOfVector !as0
= Source
{ init = return 0
, pull = \ix -> if ix >= Generic.length as0
then return (Nothing, ix)
else return (Just $ Generic.unsafeIndex as0 ix, ix + 1)
, done = \_ -> return ()
}
{-# INLINE sourceOfVectorFlip #-}
sourceOfVectorFlip :: (Monad m, Generic.Vector v a) => v a -> Source m a
sourceOfVectorFlip !as0
= Source
{ init = return 0
, pull = \ix -> if ix < Generic.length as0
then return (Just $ Generic.unsafeIndex as0 ix, ix + 1)
else return (Nothing, ix)
, done = \_ -> return ()
}
---------------------
{-
benchmarking sourceOfVector with ~80MB data
time 15.26 ms (15.11 ms .. 15.40 ms)
1.000 R² (0.999 R² .. 1.000 R²)
mean 15.31 ms (15.18 ms .. 15.81 ms)
std dev 573.3 μs (123.6 μs .. 1.164 ms)
variance introduced by outliers: 11% (moderately inflated)
benchmarking sourceOfVector with ~80MB data
time 19.63 ms (19.22 ms .. 20.13 ms)
0.998 R² (0.996 R² .. 0.999 R²)
mean 20.60 ms (20.32 ms .. 20.80 ms)
std dev 569.2 μs (443.3 μs .. 648.4 μs)
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment