Skip to content

Instantly share code, notes, and snippets.

@wyager
Created August 1, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyager/45946f9f1531351468e4366b7ba168fa to your computer and use it in GitHub Desktop.
Save wyager/45946f9f1531351468e4366b7ba168fa to your computer and use it in GitHub Desktop.
import Criterion.Main
import Data.List as L (inits, tails)
import qualified Data.Vector as V
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Unboxed as VU
splits1, splits2, splits3 :: [a] -> [([a],[a])]
splits1 xs = zip (L.inits xs) (L.tails xs)
splits2 [] = [([],[])]
splits2 xs@(x:xs') = ([],xs) : [(x:as,bs) | (as,bs) <- splits2 xs']
splits3 = go id where
go prefix xs = (prefix [],xs) : case xs of
x:xs -> go (prefix . (x :)) xs
[] -> []
splits4 :: VG.Vector v a => v a -> [(v a, v a)]
splits4 v = [VG.splitAt i v | i <- [0..VG.length v]]
main = defaultMain
[ bench "splits1" $ nf splits1 input
, bench "splits2" $ nf splits2 input
, bench "splits3" $ nf splits3 input
, bench "splits4 (boxed)" $ nf splits4 input2
, bench "splits4 (unboxed)" $ nf splits4 input3
]
where
input :: [Int]
input = [1..1000]
input2 = VG.fromList input :: V.Vector Int
input3 = VG.fromList input :: VU.Vector Int
@noughtmare
Copy link

Use

seqList (x:xs) = x `seq` xs
seqList [] = ()

main = defaultMain
  [ bench "splits1" $ whnf (seqList . splits1) input
  , bench "splits2" $ whnf (seqList . splits2) input
  , bench "splits3" $ whnf (seqList . splits3) input
  ...

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