aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/cmd/tools/bench/wyhash.v
blob: b760ad6b55835d0a3abec83ad52df252748daba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module main

import hash.fnv1a
import hash as wyhash
import rand
import benchmark

fn main() {
	rand.seed([u32(42), 0])
	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...')
	mut checksum := u64(0)
	mut start_pos := 0
	mut bgenerating := benchmark.start()
	mut bytepile := []byte{}
	for _ in 0 .. sample_size * max_str_len {
		bytepile << byte(rand.int_in_range(40, 125))
	}
	mut str_lens := []int{}
	for _ in 0 .. sample_size {
		str_lens << rand.int_in_range(min_str_len, max_str_len)
	}
	bgenerating.measure('generating strings')
	println('Hashing each of the generated strings...')
	//
	mut bhashing_1 := benchmark.start()
	start_pos = 0
	checksum = 0
	for len in str_lens {
		end_pos := start_pos + len
		checksum ^= wyhash.wyhash_c(unsafe { &byte(bytepile.data) + start_pos }, u64(len),
			1)
		start_pos = end_pos
	}
	bhashing_1.measure('wyhash.wyhash_c  | checksum: ${checksum:22}')
	mut bhashing_2 := benchmark.start()
	start_pos = 0
	checksum = 0
	for len in str_lens {
		end_pos := start_pos + len
		checksum ^= wyhash.sum64(bytepile[start_pos..end_pos], 1)
		start_pos = end_pos
	}
	bhashing_2.measure('wyhash.sum64     | checksum: ${checksum:22}')
	mut bhashing_3 := benchmark.start()
	start_pos = 0
	checksum = 0
	for len in str_lens {
		end_pos := start_pos + len
		checksum ^= fnv1a.sum64(bytepile[start_pos..end_pos])
		start_pos = end_pos
	}
	bhashing_3.measure('fnv1a.sum64      | checksum: ${checksum:22}')
}