-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
17 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 |
---|---|---|
@@ -1,25 +1,81 @@ | ||
'use strict'; | ||
|
||
var Immutable = require('immutable'); | ||
var Map = Immutable.OrderedMap | ||
var m = new Map(); | ||
function should(b){ | ||
if (!b){ | ||
throw new Error("impossible") | ||
} | ||
var Bs_Array = require("../../lib/js/bs_Array.js"); | ||
var Bs_MapInt = require("../../lib/js/bs_MapInt.js"); | ||
var Immutable = require("immutable"); | ||
|
||
var empty = new Immutable.OrderedMap(); | ||
|
||
function ofArray(kvs) { | ||
var v = empty; | ||
for(var i = 0 ,i_finish = kvs.length - 1 | 0; i <= i_finish; ++i){ | ||
var match = kvs[i]; | ||
v = v.set(match[0], match[1]); | ||
} | ||
return v; | ||
} | ||
function test() { | ||
var count = 1000000; | ||
for(var i = 0; i < count; ++i) { | ||
m = m.set(i, i); | ||
|
||
function should(b) { | ||
if (b) { | ||
return 0; | ||
} else { | ||
throw new Error("impossible"); | ||
} | ||
for(var j = 0; j < count; ++j) { | ||
should(m.get(j) !== undefined); | ||
} | ||
|
||
var shuffledDataAdd = Bs_Array.shuffle(Bs_Array.init(1000001, (function (i) { | ||
return /* tuple */[ | ||
i, | ||
i | ||
]; | ||
}))); | ||
|
||
var shuffleRemoved = Bs_Array.shuffle(Bs_Array.init(2000001, (function (i) { | ||
return i; | ||
}))); | ||
|
||
function test() { | ||
var v = ofArray(shuffledDataAdd); | ||
for(var j = 0; j <= 1000000; ++j){ | ||
should(+v.has(j)); | ||
} | ||
for (var j = 0; j < count; ++j){ | ||
m = m.delete(j) | ||
return /* () */0; | ||
} | ||
|
||
function test2() { | ||
var v = Bs_MapInt.ofArray(shuffledDataAdd); | ||
for(var j = 0; j <= 1000000; ++j){ | ||
should(Bs_MapInt.mem(v, j)); | ||
} | ||
should(m.size === 0) | ||
return /* () */0; | ||
} | ||
|
||
test(); | ||
console.time("imm_map_bench.ml 42"); | ||
|
||
test(/* () */0); | ||
|
||
console.timeEnd("imm_map_bench.ml 42"); | ||
|
||
console.time("imm_map_bench.ml 43"); | ||
|
||
test2(/* () */0); | ||
|
||
console.timeEnd("imm_map_bench.ml 43"); | ||
|
||
var A = 0; | ||
|
||
var count = 1000000; | ||
|
||
var M = 0; | ||
|
||
exports.A = A; | ||
exports.empty = empty; | ||
exports.ofArray = ofArray; | ||
exports.should = should; | ||
exports.count = count; | ||
exports.shuffledDataAdd = shuffledDataAdd; | ||
exports.shuffleRemoved = shuffleRemoved; | ||
exports.test = test; | ||
exports.M = M; | ||
exports.test2 = test2; | ||
/* empty Not a pure module */ |
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,43 @@ | ||
|
||
type map | ||
external empty : unit -> map = "OrderedMap" [@@bs.new] [@@bs.module "immutable"] | ||
external set : map -> int -> int -> map = "set" [@@bs.send] | ||
external get : map -> int -> int option = "get" [@@bs.send] [@@bs.return undefined_to_opt] | ||
external mem : map -> int -> bool = "has" [@@bs.send] | ||
|
||
|
||
module A = Bs_Array | ||
let empty = empty () | ||
let ofArray kvs = | ||
let v = ref empty in | ||
for i = 0 to A.length kvs - 1 do | ||
let key, value = (A.unsafe_get kvs i) in | ||
v := set !v key value | ||
done; | ||
!v | ||
|
||
let should b = | ||
if not b then Js.Exn.raiseError "impossible" | ||
let count = 1_000_000 | ||
let shuffledDataAdd = (A.shuffle (A.init (count + 1) (fun[@bs] i -> (i,i)))) | ||
let shuffleRemoved = A.shuffle (A.init (2 * count + 1) (fun [@bs] i -> i )) | ||
|
||
|
||
let test () = | ||
let v = ofArray shuffledDataAdd in | ||
for j = 0 to count do | ||
should (mem v j) | ||
done | ||
|
||
module M = Bs.MapInt | ||
|
||
|
||
let test2 () = | ||
let v = M.ofArray shuffledDataAdd in | ||
for j = 0 to count do | ||
should (M.mem v j) | ||
done ; | ||
|
||
|
||
;; [%time test ()] | ||
;; [%time test2 ()] |