Skip to content

Commit

Permalink
Adding performance test for PersistentHashMap - references #2
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Dec 10, 2013
1 parent f0ec267 commit fbad4a8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
44 changes: 42 additions & 2 deletions help/PersistentHashMap.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open System

(**
PersistentHashMap
=============
=================
A Map is a collection that maps keys to values. Hash maps require keys that correctly support GetHashCode and Equals. Hash maps provide fast access (log32N hops). count is O(1).
*)
Expand Down Expand Up @@ -57,6 +57,46 @@ stringBasedMap'.["d"]
// [fsi:val it : int = 16]

(**
Bulk operations on HashMaps use an internal TransientHashMap in order to get much better performance.
Performance tests
-----------------
Bulk operations on HashMaps use an internal TransientHashMap in order to get much better performance. The following scripts shows this:
*)

let trials = 5
let r = new System.Random()

open FSharpx.Collections.TimeMeasurement

let initFSharpMapAndPersistentMapFromList n =
sprintf "Init with n = %d" n |> printInFsiTags
let list = [for i in 1..n -> r.Next(),r.Next().ToString()]

let initpersistentmap list =
let m = ref empty
for (key,value) in list do
m := add key value !m
!m

compareThreeRuntimes trials
" FSharp.Map.ofSeq" (fun () -> Map.ofSeq list)
" Multiple PersistentHashMap.add" (fun () -> initpersistentmap list)
" PersistentHashMap.ofSeq" (fun () -> ofSeq list)

initFSharpMapAndPersistentMapFromList 10000
initFSharpMapAndPersistentMapFromList 100000
initFSharpMapAndPersistentMapFromList 1000000

// [fsi:Init with n = 10000]
// [fsi: FSharp.Map.ofSeq 25.2ms]
// [fsi: Multiple PersistentHashMap.add 22.4ms]
// [fsi: PersistentHashMap.ofSeq 12.2ms]
// [fsi:Init with n = 100000]
// [fsi: FSharp.Map.ofSeq 260.6ms]
// [fsi: Multiple PersistentHashMap.add 309.8ms]
// [fsi: PersistentHashMap.ofSeq 214.0ms]
// [fsi:Init with n = 1000000]
// [fsi: FSharp.Map.ofSeq 4955.6ms]
// [fsi: Multiple PersistentHashMap.add 5770.4ms]
// [fsi: PersistentHashMap.ofSeq 3867.6ms]
18 changes: 9 additions & 9 deletions help/PersistentVector.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ open FSharpx.Collections.PersistentVector
Performance tests
-----------------
Bulk operations on HashMaps use an internal TransientHashMap in order to get much better performance. The following scripts shows this.
Bulk operations on PersistentVector use an internal TransientVector in order to get much better performance. The following scripts shows this:
*)

let c = 5
let trials = 5
let r = new System.Random()

open FSharpx.Collections.TimeMeasurement
Expand All @@ -36,9 +36,9 @@ let initArrayAndVectorFromList n =
v := conj x !v
!v

compareThreeRuntimes c
compareThreeRuntimes trials
" Array.ofSeq" (fun () -> Array.ofSeq list)
" Multiple PersistentVector.conj " (fun () -> initvector list)
" Multiple PersistentVector.conj" (fun () -> initvector list)
" PersistentVector.ofSeq" (fun () -> ofSeq list)

let lookupInArrayAndVector n count =
Expand All @@ -47,7 +47,7 @@ let lookupInArrayAndVector n count =
let array = Array.ofSeq list
let vector = ofSeq list

compareTwoRuntimes c
compareTwoRuntimes trials
" Array" (fun () -> for i in 1..count do array.[r.Next n] |> ignore)
" PersistentVector" (fun () -> for i in 1..count do nth (r.Next n) vector |> ignore)

Expand All @@ -58,7 +58,7 @@ let replaceInArrayAndVector n count =
let array = Array.ofSeq list
let vector = ofSeq list

compareTwoRuntimes c
compareTwoRuntimes trials
" Array" (fun () -> for i in 1..count do array.[r.Next n] <- r.Next())
" PersistentVector" (fun () -> for i in 1..count do update (r.Next n) (r.Next()) vector |> ignore)

Expand All @@ -78,15 +78,15 @@ replaceInArrayAndVector 10000000 10000

// [fsi:Init with n = 10000]
// [fsi: Array.ofSeq 0.0ms]
// [fsi: Multiple PersistentVector.conj 3.0ms]
// [fsi: Multiple PersistentVector.conj 3.0ms]
// [fsi: PersistentVector.ofSeq 3.0ms]
// [fsi:Init with n = 100000]
// [fsi: Array.ofSeq 0.4ms]
// [fsi: Multiple PersistentVector.conj 38.8ms]
// [fsi: Multiple PersistentVector.conj 38.8ms]
// [fsi: PersistentVector.ofSeq 17.6ms]
// [fsi:Init with n = 1000000]
// [fsi: Array.ofSeq 5.4ms]
// [fsi: Multiple PersistentVector.conj 420.6ms]
// [fsi: Multiple PersistentVector.conj 420.6ms]
// [fsi: PersistentVector.ofSeq 250.6ms]
// [fsi:10000 Lookups in size n = 10000]
// [fsi: Array 0.0ms]
Expand Down

0 comments on commit fbad4a8

Please sign in to comment.