2020-01-28 23:43:09 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 08:32:05 +02:00
|
|
|
import hash.fnv1a
|
2020-07-18 14:27:57 +02:00
|
|
|
import hash as wyhash
|
2020-04-26 08:32:05 +02:00
|
|
|
import rand
|
2020-08-03 18:25:59 +02:00
|
|
|
import benchmark
|
2020-01-28 23:43:09 +01:00
|
|
|
|
|
|
|
fn main() {
|
2020-08-03 17:47:10 +02:00
|
|
|
rand.seed([u32(42), 0])
|
2020-01-28 23:43:09 +01:00
|
|
|
sample_size := 10000000
|
|
|
|
min_str_len := 20
|
|
|
|
max_str_len := 40
|
|
|
|
println('Generating $sample_size strings between $min_str_len - $max_str_len chars long...')
|
2020-08-03 18:25:59 +02:00
|
|
|
mut checksum := u64(0)
|
|
|
|
mut start_pos := 0
|
|
|
|
mut bgenerating := benchmark.start()
|
2020-04-26 13:49:31 +02:00
|
|
|
mut bytepile := []byte{}
|
2020-01-28 23:43:09 +01:00
|
|
|
for _ in 0 .. sample_size * max_str_len {
|
2020-06-09 15:06:07 +02:00
|
|
|
bytepile << byte(rand.int_in_range(40, 125))
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|
2020-04-26 13:49:31 +02:00
|
|
|
mut str_lens := []int{}
|
2020-01-28 23:43:09 +01:00
|
|
|
for _ in 0 .. sample_size {
|
2020-06-09 15:06:07 +02:00
|
|
|
str_lens << rand.int_in_range(min_str_len, max_str_len)
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|
2020-08-03 18:25:59 +02:00
|
|
|
bgenerating.measure('generating strings')
|
2020-01-28 23:43:09 +01:00
|
|
|
println('Hashing each of the generated strings...')
|
2020-08-03 18:25:59 +02:00
|
|
|
//
|
|
|
|
mut bhashing_1 := benchmark.start()
|
|
|
|
start_pos = 0
|
|
|
|
checksum = 0
|
2020-01-28 23:43:09 +01:00
|
|
|
for len in str_lens {
|
|
|
|
end_pos := start_pos + len
|
2020-08-03 17:47:10 +02:00
|
|
|
str := string(bytepile[start_pos..end_pos], len)
|
2020-08-03 18:25:59 +02:00
|
|
|
checksum ^= wyhash.wyhash_c(&str.str, u64(str.len), 1)
|
2020-01-28 23:43:09 +01:00
|
|
|
start_pos = end_pos
|
|
|
|
}
|
2020-08-03 18:25:59 +02:00
|
|
|
bhashing_1.measure('wyhash.wyhash_c | checksum: ${checksum:22}')
|
|
|
|
mut bhashing_2 := benchmark.start()
|
2020-01-28 23:43:09 +01:00
|
|
|
start_pos = 0
|
2020-08-03 18:25:59 +02:00
|
|
|
checksum = 0
|
2020-01-28 23:43:09 +01:00
|
|
|
for len in str_lens {
|
|
|
|
end_pos := start_pos + len
|
2020-08-03 17:47:10 +02:00
|
|
|
str := string(bytepile[start_pos..end_pos], len)
|
2020-08-03 18:25:59 +02:00
|
|
|
checksum ^= wyhash.sum64_string(str, 1)
|
2020-01-28 23:43:09 +01:00
|
|
|
start_pos = end_pos
|
|
|
|
}
|
2020-08-03 18:25:59 +02:00
|
|
|
bhashing_2.measure('wyhash.sum64_string | checksum: ${checksum:22}')
|
|
|
|
mut bhashing_3 := benchmark.start()
|
2020-01-28 23:43:09 +01:00
|
|
|
start_pos = 0
|
2020-08-03 18:25:59 +02:00
|
|
|
checksum = 0
|
2020-01-28 23:43:09 +01:00
|
|
|
for len in str_lens {
|
|
|
|
end_pos := start_pos + len
|
2020-08-03 17:47:10 +02:00
|
|
|
str := string(bytepile[start_pos..end_pos], len)
|
2020-08-03 18:25:59 +02:00
|
|
|
checksum ^= fnv1a.sum64_string(str)
|
2020-01-28 23:43:09 +01:00
|
|
|
start_pos = end_pos
|
|
|
|
}
|
2020-08-03 18:25:59 +02:00
|
|
|
bhashing_3.measure('fnv1a.sum64_string | checksum: ${checksum:22}')
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|