Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

size in log(n) #107

Merged
merged 2 commits into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions bench/Bench/Data/Map.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Bench.Data.Map where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Performance.Minibench (bench, benchWith)

import Data.Tuple (Tuple(..))
import Data.List as L
import Data.Map as M

benchMap :: Eff (console :: CONSOLE) Unit
benchMap = benchSize
where
benchSize = do
let nats = L.range 0 999999
natPairs = (flip Tuple) unit <$> nats
singletonMap = M.singleton 0 unit
smallMap = M.fromFoldable $ L.take 100 natPairs
midMap = M.fromFoldable $ L.take 10000 natPairs
bigMap = M.fromFoldable $ natPairs

log "size: singleton map"
bench \_ -> M.size singletonMap

log $ "size: small map (" <> show (M.size smallMap) <> ")"
bench \_ -> M.size smallMap

log $ "size: midsize map (" <> show (M.size midMap) <> ")"
benchWith 100 \_ -> M.size midMap

log $ "size: big map (" <> show (M.size bigMap) <> ")"
benchWith 10 \_ -> M.size bigMap
10 changes: 10 additions & 0 deletions bench/Bench/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Bench.Main where

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)
import Data.Unit (Unit)

import Bench.Data.Map (benchMap)

main :: Eff (console :: CONSOLE) Unit
main = benchMap
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"purescript-st": "^3.0.0"
},
"devDependencies": {
"purescript-quickcheck": "^4.0.0"
"purescript-quickcheck": "^4.0.0",
"purescript-minibench": "^1.0.0"
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "pulp test"
"test": "pulp test",

"bench:build": "purs compile 'bench/**/*.purs' 'src/**/*.purs' 'bower_components/*/src/**/*.purs'",
"bench:run": "node -e 'require(\"./output/Bench.Main/index.js\").main()'",
"bench": "npm run bench:build && npm run bench:run"
},
"devDependencies": {
"eslint": "^3.17.1",
Expand Down
4 changes: 3 additions & 1 deletion src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ isSubmap m1 m2 = LL.all f $ (toUnfoldable m1 :: LL.List (Tuple k v))

-- | Calculate the number of key/value pairs in a map
size :: forall k v. Map k v -> Int
size = length <<< values
size Leaf = 0
size (Two m1 _ _ m2) = 1 + size m1 + size m2
size (Three m1 _ _ m2 _ _ m3) = 2 + size m1 + size m2 + size m3

-- | Apply a function of two arguments to each key/value pair, producing a new map
mapWithKey :: forall k v v'. (k -> v -> v') -> Map k v -> Map k v'
Expand Down