-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding docu and performance test for PersistentVector - references #2
- Loading branch information
Showing
7 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
(*** hide ***) | ||
#r "../build/FSharpx.Collections.dll" | ||
open System | ||
|
||
|
||
(** | ||
PersistentVector | ||
============= | ||
A Vector is a collection of values indexed by contiguous integers. Vectors support access to items by index in log32N hops. count is O(1). conj puts the item at the end of the vector. | ||
*) | ||
|
||
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. | ||
*) | ||
|
||
let c = 5 | ||
let r = new System.Random() | ||
|
||
open FSharpx.Collections.TimeMeasurement | ||
|
||
let initArrayAndVectorFromList n = | ||
sprintf "Init with n = %d" n |> printInFsiTags | ||
let list = [for i in 1..n -> r.Next()] | ||
|
||
let initvector list = | ||
let v = ref empty | ||
for x in list do | ||
v := conj x !v | ||
!v | ||
|
||
compareThreeRuntimes c | ||
" Array.ofSeq" (fun () -> Array.ofSeq list) | ||
" Multiple PersistentVector.conj " (fun () -> initvector list) | ||
" PersistentVector.ofSeq" (fun () -> ofSeq list) | ||
|
||
let lookupInArrayAndVector n count = | ||
sprintf "%d Lookups in size n = %d" count n |> printInFsiTags | ||
let list = [for i in 1..n -> r.Next()] | ||
let array = Array.ofSeq list | ||
let vector = ofSeq list | ||
|
||
compareTwoRuntimes c | ||
" 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) | ||
|
||
|
||
let replaceInArrayAndVector n count = | ||
sprintf "%d writes in size n = %d" count n |> printInFsiTags | ||
let list = [for i in 1..n -> r.Next()] | ||
let array = Array.ofSeq list | ||
let vector = ofSeq list | ||
|
||
compareTwoRuntimes c | ||
" 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) | ||
|
||
initArrayAndVectorFromList 10000 | ||
initArrayAndVectorFromList 100000 | ||
initArrayAndVectorFromList 1000000 | ||
|
||
lookupInArrayAndVector 10000 10000 | ||
lookupInArrayAndVector 100000 10000 | ||
lookupInArrayAndVector 1000000 10000 | ||
lookupInArrayAndVector 10000000 10000 | ||
|
||
replaceInArrayAndVector 10000 10000 | ||
replaceInArrayAndVector 100000 10000 | ||
replaceInArrayAndVector 1000000 10000 | ||
replaceInArrayAndVector 10000000 10000 | ||
|
||
// [fsi:Init with n = 10000] | ||
// [fsi: Array.ofSeq 0.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: PersistentVector.ofSeq 17.6ms] | ||
// [fsi:Init with n = 1000000] | ||
// [fsi: Array.ofSeq 5.4ms] | ||
// [fsi: Multiple PersistentVector.conj 420.6ms] | ||
// [fsi: PersistentVector.ofSeq 250.6ms] | ||
// [fsi:10000 Lookups in size n = 10000] | ||
// [fsi: Array 0.0ms] | ||
// [fsi: PersistentVector 0.4ms] | ||
// [fsi:10000 Lookups in size n = 100000] | ||
// [fsi: Array 0.0ms] | ||
// [fsi: PersistentVector 0.6ms] | ||
// [fsi:10000 Lookups in size n = 1000000] | ||
// [fsi: Array 0.0ms] | ||
// [fsi: PersistentVector 2.0ms] | ||
// [fsi:10000 Lookups in size n = 10000000] | ||
// [fsi: Array 0.0ms] | ||
// [fsi: PersistentVector 3.4ms] | ||
// [fsi:10000 writes in size n = 10000] | ||
// [fsi: Array 0.2ms] | ||
// [fsi: PersistentVector 11.4ms] | ||
// [fsi:10000 writes in size n = 100000] | ||
// [fsi: Array 0.2ms] | ||
// [fsi: PersistentVector 11.2ms] | ||
// [fsi:10000 writes in size n = 1000000] | ||
// [fsi: Array 0.2ms] | ||
// [fsi: PersistentVector 15.0ms] | ||
// [fsi:10000 writes in size n = 10000000] | ||
// [fsi: Array 1.0ms] | ||
// [fsi: PersistentVector 33.8ms] |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module FSharpx.Collections.TimeMeasurement | ||
|
||
/// Stops the runtime for a given function | ||
let stopTime f = | ||
let sw = new System.Diagnostics.Stopwatch() | ||
sw.Start() | ||
let result = f() | ||
sw.Stop() | ||
result,float sw.ElapsedMilliseconds | ||
|
||
/// Stops the average runtime for a given function and applies it the given count | ||
let stopAverageTime count f = | ||
let sw = new System.Diagnostics.Stopwatch() | ||
let list = [1..count] | ||
sw.Start() | ||
let results = List.map (fun _ -> f()) list | ||
sw.Stop() | ||
results,float sw.ElapsedMilliseconds / float count | ||
|
||
let printInFsiTags s = printfn " [fsi:%s]" s | ||
|
||
/// Stops the average runtime for a given function and applies it the given count | ||
/// Afterwards it reports it with the given description | ||
let stopAndReportAvarageTime count desc f = | ||
let results,time = stopAverageTime count f | ||
sprintf "%s %Ams" desc time |> printInFsiTags | ||
results,time | ||
|
||
/// Stops the average runtime for the given functions | ||
/// Afterwards it reports it with the given descriptions | ||
let compareTwoRuntimes count desc1 f1 desc2 f2 = | ||
let _,time1 = stopAndReportAvarageTime count desc1 f1 | ||
let _,time2 = stopAndReportAvarageTime count desc2 f2 | ||
() | ||
|
||
/// Stops the average runtime for the given functions | ||
/// Afterwards it reports it with the given descriptions | ||
let compareThreeRuntimes count desc1 f1 desc2 f2 desc3 f3 = | ||
let _,time1 = stopAndReportAvarageTime count desc1 f1 | ||
let _,time2 = stopAndReportAvarageTime count desc2 f2 | ||
let _,time3 = stopAndReportAvarageTime count desc3 f3 | ||
() |