Skip to content

Commit 5a6a974

Browse files
committed
Add insertWith and insertWithKey benchmarks.
insertWith is implemented using insertWithKey so other than noise they should have similar results. benchmarking HashMap/insertWith/String time 1.932 ms (1.771 ms .. 2.124 ms) 0.954 R² (0.923 R² .. 0.983 R²) mean 1.750 ms (1.664 ms .. 1.872 ms) std dev 301.9 μs (227.4 μs .. 410.5 μs) variance introduced by outliers: 87% (severely inflated) benchmarking HashMap/insertWith/ByteString time 2.272 ms (2.194 ms .. 2.347 ms) 0.978 R² (0.951 R² .. 0.994 R²) mean 1.840 ms (1.744 ms .. 1.962 ms) std dev 331.1 μs (266.5 μs .. 486.0 μs) variance introduced by outliers: 88% (severely inflated) benchmarking HashMap/insertWith/Int time 1.321 ms (1.241 ms .. 1.454 ms) 0.955 R² (0.932 R² .. 0.976 R²) mean 1.365 ms (1.316 ms .. 1.420 ms) std dev 178.3 μs (150.3 μs .. 230.6 μs) variance introduced by outliers: 81% (severely inflated) benchmarking HashMap/insertWithKey/String time 1.783 ms (1.714 ms .. 1.900 ms) 0.959 R² (0.934 R² .. 0.983 R²) mean 1.614 ms (1.526 ms .. 1.747 ms) std dev 289.4 μs (217.9 μs .. 372.9 μs) variance introduced by outliers: 89% (severely inflated) benchmarking HashMap/insertWithKey/ByteString time 1.647 ms (1.605 ms .. 1.688 ms) 0.992 R² (0.983 R² .. 0.997 R²) mean 1.563 ms (1.524 ms .. 1.644 ms) std dev 164.4 μs (85.16 μs .. 308.3 μs) variance introduced by outliers: 71% (severely inflated) benchmarking HashMap/insertWithKey/Int time 1.313 ms (1.289 ms .. 1.335 ms) 0.994 R² (0.990 R² .. 0.997 R²) mean 1.289 ms (1.270 ms .. 1.338 ms) std dev 83.74 μs (50.75 μs .. 157.2 μs) variance introduced by outliers: 49% (moderately inflated)
1 parent f9c1e89 commit 5a6a974

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

benchmarks/Benchmarks.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ main = do
217217
, bench "ByteString" $ whnf (insert elemsBS) hmbs
218218
, bench "Int" $ whnf (insert elemsI) hmi
219219
]
220+
, bgroup "insertWith"
221+
[ bench "String" $ whnf (insertWith elems) HM.empty
222+
, bench "ByteString" $ whnf (insertWith elemsBS) HM.empty
223+
, bench "Int" $ whnf (insertWith elemsI) HM.empty
224+
]
225+
, bgroup "insertWithKey"
226+
[ bench "String" $ whnf (insertWithKey elems) HM.empty
227+
, bench "ByteString" $ whnf (insertWithKey elemsBS) HM.empty
228+
, bench "Int" $ whnf (insertWithKey elemsI) HM.empty
229+
]
220230
, bgroup "delete"
221231
[ bench "String" $ whnf (delete keys) hm
222232
, bench "ByteString" $ whnf (delete keysBS) hmbs
@@ -302,6 +312,27 @@ insert xs m0 = foldl' (\m (k, v) -> HM.insert k v m) m0 xs
302312
{-# SPECIALIZE insert :: [(BS.ByteString, Int)] -> HM.HashMap BS.ByteString Int
303313
-> HM.HashMap BS.ByteString Int #-}
304314

315+
insertWith :: (Eq k, Hashable k) => [(k, Int)] -> HM.HashMap k Int
316+
-> HM.HashMap k Int
317+
insertWith xs m0 = foldl' (\m (k, v) -> HM.insertWith (+) k v m) m0 xs
318+
{-# SPECIALIZE insertWith :: [(Int, Int)] -> HM.HashMap Int Int
319+
-> HM.HashMap Int Int #-}
320+
{-# SPECIALIZE insertWith :: [(String, Int)] -> HM.HashMap String Int
321+
-> HM.HashMap String Int #-}
322+
{-# SPECIALIZE insertWith :: [(BS.ByteString, Int)] -> HM.HashMap BS.ByteString Int
323+
-> HM.HashMap BS.ByteString Int #-}
324+
325+
insertWithKey :: (Eq k, Hashable k) => [(k, Int)] -> HM.HashMap k Int
326+
-> HM.HashMap k Int
327+
insertWithKey xs m0 = foldl' (\m (k, v) -> HM.insertWithKey f k v m) m0 xs
328+
where f = const (+)
329+
{-# SPECIALIZE insertWithKey :: [(Int, Int)] -> HM.HashMap Int Int
330+
-> HM.HashMap Int Int #-}
331+
{-# SPECIALIZE insertWithKey :: [(String, Int)] -> HM.HashMap String Int
332+
-> HM.HashMap String Int #-}
333+
{-# SPECIALIZE insertWithKey :: [(BS.ByteString, Int)] -> HM.HashMap BS.ByteString Int
334+
-> HM.HashMap BS.ByteString Int #-}
335+
305336
delete :: (Eq k, Hashable k) => [k] -> HM.HashMap k Int -> HM.HashMap k Int
306337
delete xs m0 = foldl' (\m k -> HM.delete k m) m0 xs
307338
{-# SPECIALIZE delete :: [Int] -> HM.HashMap Int Int -> HM.HashMap Int Int #-}

0 commit comments

Comments
 (0)