Skip to content

Instantly share code, notes, and snippets.

@michaelt
Created November 2, 2015 22:06
Show Gist options
  • Save michaelt/1281265722e88b0a7425 to your computer and use it in GitHub Desktop.
Save michaelt/1281265722e88b0a7425 to your computer and use it in GitHub Desktop.
https://github.com/jwiegley/streaming-tests modified for use with `streaming` `io-streams` `machines` etc.
module Main where
import qualified Streaming.Prelude as Str
import qualified System.IO.Streams as IOS
import qualified ListT as ListT
import Data.Conduit as C
import Data.Conduit.Combinators as C
import qualified Data.Machine as M
import qualified Data.Machine.Runner as M
import Pipes as P
import qualified Pipes.Prelude as P
import Conduit.Simple as S
import Control.Exception
import Criterion.Main
import Data.Function ((&))
import Control.Monad
main :: IO ()
main = do
defaultMain [
bgroup "basic" [bench "stream" $ nfIO stream_basic
, bench "conduit" $ nfIO conduit_basic
, bench "simple-conduit" $ nfIO simple_conduit_basic
, bench "pipes" $ nfIO pipes_basic
, bench "data-list" $ nfIO list_basic
, bench "iostreams" $ nfIO iostreams_basic
, bench "machines" $ nfIO machine_basic
]
]
machine_basic = do
xs <- M.runT $ M.enumerateFromTo 1 limit
M.~> M.filtered even
M.~> M.auto (+1)
M.~> M.dropping 1000
M.~> M.auto (+1)
M.~> M.filtered (\x -> x `mod` 2 == 0)
assert (Prelude.length xs == len) $
return (Prelude.length xs)
limit = 1000000 :: Int
len = 499000 :: Int
pipes_basic :: IO Int
pipes_basic = do
xs <- P.toListM $ P.each [1..limit]
>-> P.filter even
>-> P.map (+1)
>-> P.drop 1000
>-> P.map (+1)
>-> P.filter (\x -> x `mod` 2 == 0)
assert (Prelude.length xs == len) $
return (Prelude.length xs)
conduit_basic :: IO Int
conduit_basic = do
xs <- C.yieldMany [1..limit]
C.$= C.filter even
C.$= C.map ((+1) :: Int -> Int)
C.$= (C.drop 1000 >> C.awaitForever C.yield)
C.$= C.map ((+1) :: Int -> Int)
C.$= C.filter (\x -> x `mod` 2 == 0)
C.$$ C.sinkList
assert (Prelude.length xs == len) $
return (Prelude.length (xs :: [Int]))
simple_conduit_basic :: IO Int
simple_conduit_basic = do
xs <- S.sourceList [1..limit]
S.$= S.filterC even
S.$= S.mapC ((+1) :: Int -> Int)
S.$= S.dropC 1000
S.$= S.mapC ((+1) :: Int -> Int)
S.$= S.filterC (\x -> x `mod` 2 == 0)
S.$$ S.sinkList
assert (Prelude.length xs == len) $
return (Prelude.length (xs :: [Int]))
list_basic :: IO Int
list_basic = do
xs <- Prelude.mapM return [1..limit]
>>= \ns -> return $
ns & Prelude.filter even
& Prelude.map (+1)
& Prelude.drop 1000
& Prelude.map (+1)
& Prelude.filter (\x -> x `mod` 2 == 0)
assert (Prelude.length xs == len) $
return (Prelude.length (xs :: [Int]))
stream_basic :: IO Int
stream_basic = do
xs <- Str.toList_
. Str.filter (\x -> x `mod` 2 == 0)
. Str.map (+1)
. Str.drop 1000
. Str.map (+1)
. Str.filter even
$ Str.each [1..limit]
assert (Prelude.length xs == len) $
return (Prelude.length (xs :: [Int]))
iostreams_basic :: IO Int
iostreams_basic = do
s0 <- IOS.fromList [1..limit]
s1 <- IOS.filter even s0
s2 <- IOS.map (+1) s1
s3 <- IOS.drop 1000 s2
s4 <- IOS.map (+1) s3
s5 <- IOS.filter (\x -> x `mod` 2 == 0) s4
xs <- IOS.toList s5
assert (Prelude.length xs == len) $
return (Prelude.length (xs :: [Int]))
@michaelt
Copy link
Author

michaelt commented Nov 2, 2015

benchmarking basic/stream
time                 85.45 ms   (81.63 ms .. 89.32 ms)
                     0.994 R²   (0.982 R² .. 0.999 R²)
mean                 86.53 ms   (84.16 ms .. 90.51 ms)
std dev              4.987 ms   (2.301 ms .. 7.906 ms)
variance introduced by outliers: 18% (moderately inflated)

benchmarking basic/conduit
time                 101.3 ms   (88.77 ms .. 111.3 ms)
                     0.976 R²   (0.911 R² .. 0.996 R²)
mean                 95.56 ms   (84.90 ms .. 103.6 ms)
std dev              13.76 ms   (8.210 ms .. 21.79 ms)
variance introduced by outliers: 43% (moderately inflated)

benchmarking basic/simple-conduit
time                 199.2 ms   (174.1 ms .. 215.6 ms)
                     0.993 R²   (0.978 R² .. 1.000 R²)
mean                 198.4 ms   (190.0 ms .. 202.2 ms)
std dev              7.091 ms   (1.565 ms .. 10.000 ms)
variance introduced by outliers: 14% (moderately inflated)

benchmarking basic/pipes
time                 211.7 ms   (180.8 ms .. 232.7 ms)
                     0.991 R²   (0.974 R² .. 1.000 R²)
mean                 207.7 ms   (199.1 ms .. 218.7 ms)
std dev              12.34 ms   (5.989 ms .. 17.67 ms)
variance introduced by outliers: 15% (moderately inflated)

benchmarking basic/data-list
time                 202.7 ms   (186.5 ms .. 225.5 ms)
                     0.990 R²   (0.970 R² .. 1.000 R²)
mean                 199.3 ms   (188.4 ms .. 207.4 ms)
std dev              11.67 ms   (6.966 ms .. 15.11 ms)
variance introduced by outliers: 15% (moderately inflated)

benchmarking basic/iostreams
time                 265.7 ms   (247.2 ms .. 284.8 ms)
                     0.997 R²   (0.990 R² .. 1.000 R²)
mean                 265.6 ms   (261.9 ms .. 272.8 ms)
std dev              7.094 ms   (146.8 μs .. 8.387 ms)
variance introduced by outliers: 16% (moderately inflated)

benchmarking basic/machines
time                 1.123 s    (NaN s .. 1.206 s)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 1.134 s    (1.114 s .. 1.145 s)
std dev              17.29 ms   (0.0 s .. 19.07 ms)
variance introduced by outliers: 19% (moderately inflated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment