forked from dfinity/canister-profiling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve benchmark infrastructure and add comparison with unoptimised …
…implementation
- Loading branch information
1 parent
643266c
commit 68a6793
Showing
4 changed files
with
78 additions
and
10 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,58 @@ | ||
import Map "mo:base/PersistentOrderedMapUnOpt"; | ||
import Nat64 "mo:base/Nat64"; | ||
import Nat32 "mo:base/Nat32"; | ||
import Iter "mo:base/Iter"; | ||
import Option "mo:base/Option"; | ||
import Random "random"; | ||
import Profiling "../../../utils/motoko/Profiling"; | ||
|
||
actor { | ||
stable let profiling = Profiling.init(); | ||
|
||
let mapOps = Map.MapOps<Nat64>(Nat64.compare); | ||
stable var rbMap = Map.empty<Nat64, Nat64>(); | ||
let rand = Random.new(null, 42); | ||
|
||
public func generate(size: Nat32) : async () { | ||
let rand = Random.new(?size, 1); | ||
let iter = Iter.map<Nat64, (Nat64, Nat64)>(rand, func x = (x, x)); | ||
rbMap := mapOps.fromIter(iter); | ||
}; | ||
public query func get_mem() : async (Nat,Nat,Nat) { | ||
Random.get_memory() | ||
}; | ||
public func batch_get(n : Nat) : async () { | ||
for (_ in Iter.range(1, n)) { | ||
ignore mapOps.get(rbMap, Option.get<Nat64>(rand.next(), 0)); | ||
} | ||
}; | ||
public func batch_put(n : Nat) : async () { | ||
for (_ in Iter.range(1, n)) { | ||
let k = Option.get<Nat64>(rand.next(), 0); | ||
rbMap := mapOps.put(rbMap, k, k); | ||
} | ||
}; | ||
public func batch_remove(n : Nat) : async () { | ||
let rand = Random.new(null, 1); | ||
for (_ in Iter.range(1, n)) { | ||
rbMap := mapOps.delete(rbMap, Option.get<Nat64>(rand.next(), 0)); | ||
} | ||
}; | ||
public func foldLeft() : async () { | ||
ignore Map.foldLeft<Nat64, Nat64, Nat64>(rbMap, 0, func (x, y, acc) {x + y + acc}); | ||
}; | ||
public func foldRight() : async () { | ||
ignore Map.foldRight<Nat64, Nat64, Nat64>(rbMap, 0, func (x, y, acc) {x + y + acc}); | ||
}; | ||
public func mapfilter() : async () { | ||
ignore mapOps.mapFilter<Nat64, Nat64>(rbMap, func (key, value) { | ||
switch (key % 2) { | ||
case 1 { null }; | ||
case _ { ?value } | ||
}; | ||
}) | ||
}; | ||
public func map() : async () { | ||
ignore Map.map<Nat64, Nat64, Nat64>(rbMap, func (key, value) {key + value}) | ||
}; | ||
} |
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