1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

bench: add bench_random_number_generation.v

This commit is contained in:
Delyan Angelov 2023-01-19 15:19:29 +02:00
parent 12f9e922bc
commit 550cae931f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -0,0 +1,50 @@
import os
import rand
import rand.wyrand
import rand.musl
import rand.pcg32
import benchmark
const max_iterations = os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int()
fn main() {
mut sum := 0
mut bigsum := u64(0)
mut b := benchmark.start()
C.srand(7868768)
for _ in 0 .. max_iterations {
sum += C.rand()
}
b.measure('C.rand called ${max_iterations} times, sum: ${sum}')
rand.seed([u32(545123), 87834597])
for _ in 0 .. max_iterations {
bigsum += rand.u64()
}
b.measure('rand.u64 called ${max_iterations} times, bigsum: ${bigsum}')
bigsum = 0
mut wy := &wyrand.WyRandRNG{}
wy.seed([u32(2345345), 8787671])
for _ in 0 .. max_iterations {
bigsum += wy.u64()
}
b.measure('wy.u64 called ${max_iterations} times, bigsum: ${bigsum}')
bigsum = 0
mut mu := &musl.MuslRNG{}
mu.seed([u32(2345345)])
for _ in 0 .. max_iterations {
bigsum += mu.u64()
}
b.measure('mu.u64 called ${max_iterations} times, bigsum: ${bigsum}')
bigsum = 0
mut pc := &pcg32.PCG32RNG{}
pc.seed([u32(2345345), 34345, 77634, 12381])
for _ in 0 .. max_iterations {
bigsum += pc.u64()
}
b.measure('pc.u64 called ${max_iterations} times, bigsum: ${bigsum}')
}