Created
August 1, 2019 10:44
-
-
Save wyager/45946f9f1531351468e4366b7ba168fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use