| 1 | module main |
| 2 | |
| 3 | import hash.fnv1a |
| 4 | import hash as wyhash |
| 5 | import rand |
| 6 | import benchmark |
| 7 | |
| 8 | fn main() { |
| 9 | rand.seed([u32(42), 0]) |
| 10 | sample_size := 10000000 |
| 11 | min_str_len := 20 |
| 12 | max_str_len := 40 |
| 13 | println('Generating ${sample_size} strings between ${min_str_len} - ${max_str_len} chars long...') |
| 14 | mut checksum := u64(0) |
| 15 | mut start_pos := 0 |
| 16 | mut bgenerating := benchmark.start() |
| 17 | mut bytepile := []u8{} |
| 18 | for _ in 0 .. sample_size * max_str_len { |
| 19 | bytepile << u8(rand.int_in_range(40, 125) or { 40 }) |
| 20 | } |
| 21 | mut str_lens := []int{} |
| 22 | for _ in 0 .. sample_size { |
| 23 | str_lens << rand.int_in_range(min_str_len, max_str_len) or { min_str_len } |
| 24 | } |
| 25 | bgenerating.measure('generating strings') |
| 26 | println('Hashing each of the generated strings...') |
| 27 | |
| 28 | mut bhashing_1 := benchmark.start() |
| 29 | start_pos = 0 |
| 30 | checksum = 0 |
| 31 | for len in str_lens { |
| 32 | end_pos := start_pos + len |
| 33 | checksum ^= wyhash.wyhash_c(unsafe { &u8(bytepile.data) + start_pos }, u64(len), 1) |
| 34 | start_pos = end_pos |
| 35 | } |
| 36 | bhashing_1.measure('wyhash.wyhash_c | checksum: ${checksum:22}') |
| 37 | mut bhashing_2 := benchmark.start() |
| 38 | start_pos = 0 |
| 39 | checksum = 0 |
| 40 | for len in str_lens { |
| 41 | end_pos := start_pos + len |
| 42 | checksum ^= wyhash.sum64(bytepile[start_pos..end_pos], 1) |
| 43 | start_pos = end_pos |
| 44 | } |
| 45 | bhashing_2.measure('wyhash.sum64 | checksum: ${checksum:22}') |
| 46 | mut bhashing_3 := benchmark.start() |
| 47 | start_pos = 0 |
| 48 | checksum = 0 |
| 49 | for len in str_lens { |
| 50 | end_pos := start_pos + len |
| 51 | checksum ^= fnv1a.sum64(bytepile[start_pos..end_pos]) |
| 52 | start_pos = end_pos |
| 53 | } |
| 54 | bhashing_3.measure('fnv1a.sum64 | checksum: ${checksum:22}') |
| 55 | } |
| 56 | |