Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add benchmarks comparing vs. IntSet

  • Loading branch information...
commit aee63465387ee9aec7eca8c28256a1722522b922 1 parent 909d516
@lowasser authored
View
52 IntBench/Base.hs
@@ -0,0 +1,52 @@
+{-# LANGUAGE BangPatterns, RecordWildCards #-}
+{-# OPTIONS -fasm -funbox-strict-fields #-}
+module IntBench.Base where
+
+import Control.Monad.ST
+import qualified System.Random.MWC as R
+import Data.Vector.Primitive
+import qualified Data.Vector.Primitive.Mutable as M
+import Data.Vector.Algorithms.Radix
+
+import Data.Bits
+import Prelude hiding (elem)
+
+minInt, maxInt :: Int
+minInt = - bit 20
+maxInt = bit 20
+
+count :: Int
+count = bit 15
+
+data TestCase = TestCase {
+ memberVal :: !Int,
+ nonMemberVal :: !Int,
+ vector1 :: !(Vector Int),
+ vector2 :: !(Vector Int),
+ sortedVector :: !(Vector Int)}
+
+testCase :: TestCase
+testCase = runST $ do
+ gen <- R.create
+ !mv1 <- M.new count
+ !mv2 <- M.new count
+ !mv3 <- M.new count
+ forM_ (enumFromN 0 count) (\ i -> do
+ !r1 <- R.uniformR (minInt, maxInt) gen
+ !r2 <- R.uniformR (minInt, maxInt) gen
+ !r3 <- R.uniformR (minInt, maxInt) gen
+ M.write mv1 i r1
+ M.write mv2 i r2
+ M.write mv3 i r3)
+ sort mv3
+ vector1 <- unsafeFreeze mv1
+ vector2 <- unsafeFreeze mv2
+ sortedVector <- unsafeFreeze mv3
+ let memberVal = vector1 ! 0
+ let findNonMember = do
+ i <- R.uniformR (minInt, maxInt) gen
+ if i `elem` vector1 then findNonMember else return i
+ nonMemberVal <- findNonMember
+ return TestCase{..}
+
+
View
60 IntBench/IntSet.hs
@@ -0,0 +1,60 @@
+{-# LANGUAGE NamedFieldPuns, RecordWildCards #-}
+{-# OPTIONS -fasm #-}
+module IntBench.IntSet (main) where
+
+import Criterion.Main
+import Control.DeepSeq
+import Data.IntSet
+import IntBench.Base
+import qualified Data.Vector.Primitive as P
+import qualified Progression.Main as Prog
+import Prelude hiding (filter)
+
+tSortBench TestCase{vector1} = toList (fromList (P.toList vector1))
+
+tIntersectBench (set1, set2) = intersection set1 set2 `seq` ()
+
+tLookupBench (set, m, nm) = (m `member` set, nm `member` set)
+
+tUnionBench (set1, set2) = union set1 set2 `seq` ()
+
+tDiffBench (set1, set2) = difference set1 set2 `seq` ()
+
+tFilterBench set1 = filter even set1 `seq` ()
+
+tSplitBench (set1, nm) = case split nm set1 of
+ (l, r) -> l `seq` r `seq` ()
+
+tEnds set = case deleteFindMin set of
+ (l, set') -> case deleteFindMax set' of
+ (r, set'') -> set'' `seq` l - r
+
+tFromList TestCase{vector1} = fromList (P.toList vector1) `seq` ()
+tToList set = sum (toList set)
+
+tFAL TestCase{sortedVector} = fromAscList (P.toList sortedVector) `seq` ()
+
+tInsert (set, nm) = insert nm set `seq` ()
+
+tNeighborhood (set, i) = case splitMember i set of
+ (l, x, r) -> (findMax l, x, findMin r)
+nf' f a = f a `deepseq` nf f a
+
+tBenches = bgroup ""
+ [bench "Lookup" (nf' tLookupBench (set1, m, nm)),
+ set2 `seq` bench "Intersect" (nf' tIntersectBench (set1, set2)),
+ bench "Sort" (nf' tSortBench tc),
+ bench "Union" (nf' tUnionBench (set1, set2)),
+ bench "Difference" (nf' tDiffBench (set1, set2)),
+ bench "Filter" (nf' tFilterBench set1),
+ bench "Split" (nf' tSplitBench (set1, nm)),
+ bench "Neighborhood" (nf' tNeighborhood (set1, nm)),
+ bench "Min/Max" (nf' tEnds set1),
+ bench "ToList" (nf' tToList set1),
+ bench "Insert" (nf' tInsert (set1, nm)),
+ bench "FromList" (nf' tFromList tc),
+ bench "FromAscList" (nf' tFAL tc)]
+ where tc@TestCase{..} = testCase; !set1 = fromList (P.toList vector1); !set2 = fromList (P.toList vector2)
+ !nm = nonMemberVal; !m = memberVal
+main :: IO ()
+main = Prog.defaultMain tBenches
View
60 IntBench/Set.hs
@@ -0,0 +1,60 @@
+{-# LANGUAGE NamedFieldPuns, RecordWildCards #-}
+{-# OPTIONS -fasm #-}
+module IntBench.Set (main) where
+
+import Criterion.Main
+import Control.DeepSeq
+import Data.Set
+import IntBench.Base
+import qualified Data.Vector.Primitive as P
+import qualified Progression.Main as Prog
+import Prelude hiding (filter)
+
+tSortBench TestCase{vector1} = toList (fromList (P.toList vector1))
+
+tIntersectBench (set1, set2) = size (intersection set1 set2)
+
+tLookupBench (set, m, nm) = (m `member` set, nm `member` set)
+
+tUnionBench (set1, set2) = size set1 + size set2 - size (union set1 set2)
+
+tDiffBench (set1, set2) = size set1 - size (difference set1 set2)
+
+tFilterBench set1 = size (filter even set1)
+
+tSplitBench (set1, nm) = case split nm set1 of
+ (l, r) -> size l - size r
+
+tEnds set = case deleteFindMin set of
+ (l, set') -> case deleteFindMax set' of
+ (r, set'') -> size set'' + l - r
+
+tFromList TestCase{vector1} = size (fromList (P.toList vector1))
+tToList set = sum (toList set)
+
+tFAL TestCase{sortedVector} = size (fromAscList (P.toList sortedVector))
+
+tInsert (set, nm) = size (insert nm set)
+
+tNeighborhood (set, i) = case splitMember i set of
+ (l, x, r) -> (findMax l, x, findMin r)
+nf' f a = f a `deepseq` nf f a
+
+tBenches = bgroup ""
+ [bench "Lookup" (nf' tLookupBench (set1, m, nm)),
+ set2 `seq` bench "Intersect" (nf' tIntersectBench (set1, set2)),
+ bench "Sort" (nf' tSortBench tc),
+ bench "Union" (nf' tUnionBench (set1, set2)),
+ bench "Difference" (nf' tDiffBench (set1, set2)),
+ bench "Filter" (nf' tFilterBench set1),
+ bench "Split" (nf' tSplitBench (set1, nm)),
+ bench "Neighborhood" (nf' tNeighborhood (set1, nm)),
+ bench "Min/Max" (nf' tEnds set1),
+ bench "ToList" (nf' tToList set1),
+ bench "Insert" (nf' tInsert (set1, nm)),
+ bench "FromList" (nf' tFromList tc),
+ bench "FromAscList" (nf' tFAL tc)]
+ where tc@TestCase{..} = testCase; !set1 = fromList (P.toList vector1); !set2 = fromList (P.toList vector2)
+ !nm = nonMemberVal; !m = memberVal
+main :: IO ()
+main = Prog.defaultMain tBenches
View
61 IntBench/Trie.hs
@@ -0,0 +1,61 @@
+{-# LANGUAGE NamedFieldPuns, RecordWildCards #-}
+module IntBench.Trie (main) where
+
+import Criterion.Main
+import Control.DeepSeq
+import Data.TrieSet
+import IntBench.Base
+import qualified Data.Vector.Primitive as P
+import qualified Progression.Main as Prog
+import Prelude hiding (filter)
+
+tSortBench TestCase{vector1} = toList (fromVector vector1)
+
+tIntersectBench (set1, set2) = size (intersection set1 set2)
+
+tLookupBench (set, m, nm) = (m `member` set, nm `member` set)
+
+tUnionBench (set1, set2) = size set1 + size set2 - size (union set1 set2)
+
+tDiffBench (set1, set2) = size set1 - size (difference set1 set2)
+
+tFilterBench set1 = size (filter even set1)
+
+tSplitBench (set1, nm) = case split nm set1 of
+ (l, r) -> size l - size r
+
+tEnds set = case deleteFindMin set of
+ (l, set') -> case deleteFindMax set' of
+ (r, set'') -> size set'' + l - r
+
+tFromList TestCase{vector1} = size (fromVector vector1)
+tToList set = sum (toList set)
+
+tFAL TestCase{sortedVector} = size (fromAscVector sortedVector)
+
+tInsert (set, nm) = size (insert nm set)
+tIndex set = elemAt (31415926 `rem` size set) set
+
+tNeighborhood (set, i) = case splitMember i set of
+ (l, x, r) -> (findMax l, x, findMin r)
+nf' f a = f a `deepseq` nf f a
+
+tBenches = bgroup ""
+ [bench "Lookup" (nf' tLookupBench (set1, m, nm)),
+ set2 `seq` bench "Intersect" (nf' tIntersectBench (set1, set2)),
+ bench "Sort" (nf' tSortBench tc),
+ bench "Union" (nf' tUnionBench (set1, set2)),
+ bench "Difference" (nf' tDiffBench (set1, set2)),
+ bench "Filter" (nf' tFilterBench set1),
+ bench "Split" (nf' tSplitBench (set1, nm)),
+ bench "Neighborhood" (nf' tNeighborhood (set1, nm)),
+ bench "Index" (nf' tIndex set1),
+ bench "Min/Max" (nf' tEnds set1),
+ bench "ToList" (nf' tToList set1),
+ bench "Insert" (nf' tInsert (set1, nm)),
+ bench "FromList" (nf' tFromList tc),
+ bench "FromAscList" (nf' tFAL tc)]
+ where tc@TestCase{..} = testCase; !set1 = fromList (P.toList vector1); !set2 = fromList (P.toList vector2)
+ !nm = nonMemberVal; !m = memberVal
+main :: IO ()
+main = Prog.defaultMain tBenches
View
25 Makefile
@@ -1,6 +1,6 @@
PLOT_FILE := bench.png
-.PHONY : opt bench clean install prof test debug benchprof threadscope $(PLOT_FILE) BenchmarkP.prof
+.PHONY : opt bench clean install prof test debug benchprof threadscope $(PLOT_FILE) BenchmarkP.prof intbench
GHC_BIN := ghc-7.0.1
FAST_DIR := out/fast
@@ -39,6 +39,29 @@ testdbg :: TestsP
bench : SAMPLES = 30
bench : $(PLOT_FILE)
+intbench : SAMPLES = 30
+intbench : bench-IntBench-Trie.csv bench-IntBench-Set.csv bench-IntBench-IntSet.csv
+ ./IntBench-Trie --mode=graph --group=$(PROGRESSION_GROUP) --compare="IntBench-Trie,IntBench-Set,IntBench-IntSet" --plot=intbench.png \
+ --plot-log-y
+
+bench-IntBench-Trie.csv : IntBench-Trie
+ ./$< +RTS $(RTS_OPTS) -RTS --name="$<" $(BENCH_OPTS)
+
+bench-IntBench-Set.csv : IntBench-Set
+ ./$< +RTS $(RTS_OPTS) -RTS --name="$<" $(BENCH_OPTS)
+
+bench-IntBench-IntSet.csv : IntBench-IntSet
+ ./$< +RTS $(RTS_OPTS) -RTS --name="$<" $(BENCH_OPTS)
+
+IntBench-Trie : opt IntBench/Trie.hs IntBench/Base.hs
+ $(GHC_BIN) $(OPTIMIZED_GHC_OPTS) -w IntBench.Trie -main-is IntBench.Trie -o IntBench-Trie
+
+IntBench-Set : IntBench/Set.hs IntBench/Base.hs
+ $(GHC_BIN) $(OPTIMIZED_GHC_OPTS) -w IntBench.Set -main-is IntBench.Set -o IntBench-Set
+
+IntBench-IntSet : IntBench/IntSet.hs IntBench/Base.hs
+ $(GHC_BIN) $(OPTIMIZED_GHC_OPTS) -w IntBench.IntSet -main-is IntBench.IntSet -o IntBench-IntSet
+
BENCH_OPTS = --mode=run --group=$(PROGRESSION_GROUP) --prefixes=$(PROGRESSION_PREFIXES) \
--compare="" -- -s $(SAMPLES)
Please sign in to comment.
Something went wrong with that request. Please try again.