Skip to content

Instantly share code, notes, and snippets.

@mfelsche
Created September 7, 2020 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfelsche/da13a70d6e4048bd70800a46f23b7ef4 to your computer and use it in GitHub Desktop.
Save mfelsche/da13a70d6e4048bd70800a46f23b7ef4 to your computer and use it in GitHub Desktop.
ponylang benchmark for checking performance of hashing in general and some hashmap ops between master and hashing_builtin branch
use "ponybench"
use "collections"
use "random"
actor Main is BenchmarkList
new create(env: Env) =>
PonyBench(env, this)
fun tag benchmarks(bench: PonyBench) =>
bench(HashingBenchIntKeys(128))
bench(HashingBenchIntKeys(4096))
bench(HashingBenchStringKeys(1028, 64))
bench(RealHashingBench[U8]("U8"))
bench(RealHashingBench[U16]("U16"))
bench(RealHashingBench[U32]("U32"))
bench(RealHashingBench[U64]("U64"))
bench(RealHashingBench[U128]("U128"))
bench(RealHashingBench[USize]("USize"))
bench(RealHashingBench[ULong]("ULong"))
bench(StringHashingBench(8))
bench(StringHashingBench(64))
bench(StringHashingBench(512))
//bench(HashingBenchIntKeys())
//bench(HashingBenchFewStringKeys)
//bench(HashingBenchManyStringKeys)
//bench(HashingBenchClassKeys)
class iso HashingBenchIntKeys is MicroBenchmark
let _rand: Random
let _num_keys: USize
let _nums: Array[U64] ref
new iso create(num_keys: USize) =>
_num_keys = num_keys
_rand = Rand
_nums = Array[U64](_num_keys)
fun name(): String => "hashmap/update/int/" + _num_keys.string()
fun ref before_iteration() =>
try
for i in Range(0, _num_keys) do
_nums(i)? = _rand.u64()
end
end
fun ref apply() =>
let map = Map[U64, U64](_num_keys)
for i in _nums.values() do
map(i) = i + 1
end
DoNotOptimise[Map[U64, U64]](map)
DoNotOptimise.observe()
class iso HashingBenchStringKeys is MicroBenchmark
let _rand: Random
let _num_keys: USize
let _str_length: USize
let _keys: Array[String] ref
new iso create(num_keys: USize, string_length: USize) =>
_num_keys = num_keys
_str_length = string_length
_rand = Rand
_keys = Array[String](_num_keys)
fun name(): String => "hashmap/update/str/keys=" + _num_keys.string() + "/len=" + _str_length.string()
fun ref before_iteration() =>
try
for i in Range(0, _num_keys) do
let s = recover String(_str_length) end
for j in Range(0, _str_length) do
s(j)? = _rand.u8()
end
_keys(i)? = consume s
end
end
fun ref apply() =>
let map = Map[String, U64](_num_keys)
for i in _keys.values() do
map(i) = (try i(0)? else 0 end).u64()
end
DoNotOptimise[Map[String, U64]](map)
DoNotOptimise.observe()
class iso RealHashingBench[T: (Real[T] val & Int)] is MicroBenchmark
let _name: String
let random: Random = Rand
var y: U64 = 0
new iso create(name': String) =>
_name = name'
fun name(): String => "hashing/" + _name
fun ref before_iteration() =>
y = random.next()
fun apply() =>
// prevent compiler from optimizing out this operation
DoNotOptimise[USize](y.hash())
DoNotOptimise.observe()
class iso StringHashingBench is MicroBenchmark
let random: Random = Rand
let _len: USize
var _s: String = ""
new iso create(len: USize) =>
_len = len
fun config(): BenchConfig =>
BenchConfig(where max_iterations' = 10000)
fun name(): String => "hashing/str/" + _len.string()
fun ref before_iteration() =>
let s = recover String(_len) end
for i in Range(0, _len) do
try s(i)? = random.u8() end
end
_s = consume s
fun apply() =>
DoNotOptimise[USize](_s.hash())
DoNotOptimise.observe()
./stdlib_benches_hashing
Benchmark results will have their mean and median adjusted for overhead.
You may disable this with --noadjust.
Benchmark mean median deviation iterations
hashmap/update/int/128 707 ns 705 ns ±1.00% 200000
hashmap/update/int/4096 13575 ns 13513 ns ±1.98% 10000
hashmap/update/str/keys=1028/len=64 3827 ns 3789 ns ±3.01% 50000
hashing/U8 11 ns 11 ns ±1.08% 3000000
hashing/U16 10 ns 11 ns ±1.64% 3000000
hashing/U32 12 ns 11 ns ±1.38% 3000000
hashing/U64 11 ns 11 ns ±1.07% 3000000
hashing/U128 12 ns 12 ns ±1.13% 2000000
hashing/USize 12 ns 12 ns ±1.11% 3000000
hashing/ULong 11 ns 11 ns ±1.71% 3000000
hashing/str/8 23 ns 22 ns ±7.65% 10000
hashing/str/64 36 ns 35 ns ±10.75% 10000
hashing/str/512 66 ns 67 ns ±9.93% 10000
$ ./stdlib_benches_master
Benchmark results will have their mean and median adjusted for overhead.
You may disable this with --noadjust.
Benchmark mean median deviation iterations
hashmap/update/int/128 583 ns 588 ns ±3.49% 200000
hashmap/update/int/4096 9075 ns 9054 ns ±1.19% 20000
hashmap/update/str/keys=1028/len=64 3855 ns 3806 ns ±3.07% 50000
hashing/U8 4 ns 4 ns ±1.10% 3000000
hashing/U16 4 ns 5 ns ±1.42% 3000000
hashing/U32 4 ns 4 ns ±1.83% 3000000
hashing/U64 4 ns 4 ns ±1.39% 3000000
hashing/U128 4 ns 4 ns ±1.59% 3000000
hashing/USize 4 ns 4 ns ±0.75% 3000000
hashing/ULong 5 ns 4 ns ±1.99% 3000000
hashing/str/8 170 ns 169 ns ±3.93% 10000
hashing/str/64 181 ns 180 ns ±2.34% 10000
hashing/str/512 204 ns 204 ns ±4.55% 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment