aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmark/runtime/benchmark_runtime.odin
blob: 871fb05e6172365b739acf18ab54940a4ed70194 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package benchmark_runtime

import "base:runtime"
import "core:fmt"
import "core:log"
import "core:testing"
import "core:strings"
import "core:text/table"
import "core:time"

RUNS_PER_SIZE :: 2500

sizes := [?]int {
	7, 8, 9,
	15, 16, 17,
	31, 32, 33,
	63, 64, 65,
	95, 96, 97,
	128,
	256,
	512,
	1024,
	4096,
	1024 * 1024,
}

// These are the normal, unoptimized algorithms.

plain_memory_equal :: proc "contextless" (x, y: rawptr, n: int) -> bool {
	switch {
	case n == 0: return true
	case x == y: return true
	}
	a, b := ([^]byte)(x), ([^]byte)(y)
	length := uint(n)

	for i := uint(0); i < length; i += 1 {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

plain_memory_compare :: proc "contextless" (a, b: rawptr, n: int) -> int #no_bounds_check {
	switch {
	case a == b:   return 0
	case a == nil: return -1
	case b == nil: return +1
	}

	x := uintptr(a)
	y := uintptr(b)
	n := uintptr(n)

	SU :: size_of(uintptr)
	fast := n/SU + 1
	offset := (fast-1)*SU
	curr_block := uintptr(0)
	if n < SU {
		fast = 0
	}

	for /**/; curr_block < fast; curr_block += 1 {
		va := (^uintptr)(x + curr_block * size_of(uintptr))^
		vb := (^uintptr)(y + curr_block * size_of(uintptr))^
		if va ~ vb != 0 {
			for pos := curr_block*SU; pos < n; pos += 1 {
				a := (^byte)(x+pos)^
				b := (^byte)(y+pos)^
				if a ~ b != 0 {
					return -1 if (int(a) - int(b)) < 0 else +1
				}
			}
		}
	}

	for /**/; offset < n; offset += 1 {
		a := (^byte)(x+offset)^
		b := (^byte)(y+offset)^
		if a ~ b != 0 {
			return -1 if (int(a) - int(b)) < 0 else +1
		}
	}

	return 0
}

plain_memory_compare_zero :: proc "contextless" (a: rawptr, n: int) -> int #no_bounds_check {
	x := uintptr(a)
	n := uintptr(n)

	SU :: size_of(uintptr)
	fast := n/SU + 1
	offset := (fast-1)*SU
	curr_block := uintptr(0)
	if n < SU {
		fast = 0
	}

	for /**/; curr_block < fast; curr_block += 1 {
		va := (^uintptr)(x + curr_block * size_of(uintptr))^
		if va ~ 0 != 0 {
			for pos := curr_block*SU; pos < n; pos += 1 {
				a := (^byte)(x+pos)^
				if a ~ 0 != 0 {
					return -1 if int(a) < 0 else +1
				}
			}
		}
	}

	for /**/; offset < n; offset += 1 {
		a := (^byte)(x+offset)^
		if a ~ 0 != 0 {
			return -1 if int(a) < 0 else +1
		}
	}

	return 0
}

run_trial_size_cmp :: proc(p: proc "contextless" (rawptr, rawptr, int) -> $R, size: int, idx: int, runs: int, loc := #caller_location) -> (timing: time.Duration) {
	left  := make([]u8, size)
	right := make([]u8, size)
	defer {
		delete(left)
		delete(right)
	}

	right[idx] = 0x01

	accumulator: int

	watch: time.Stopwatch

	time.stopwatch_start(&watch)
	for _ in 0..<runs {
		result := p(&left[0], &right[0], size)
		when R == bool {
			assert(result == false, loc = loc)
			accumulator += 1
		} else when R == int {
			assert(result == -1, loc = loc)
			accumulator += result
		}
	}
	time.stopwatch_stop(&watch)
	timing = time.stopwatch_duration(watch)

	log.debug(accumulator)
	return
}

run_trial_size_zero :: proc(p: proc "contextless" (rawptr, int) -> int, size: int, idx: int, runs: int, loc := #caller_location) -> (timing: time.Duration) {
	data := make([]u8, size)
	defer delete(data)

	data[idx] = 0x01

	accumulator: int

	watch: time.Stopwatch

	time.stopwatch_start(&watch)
	for _ in 0..<runs {
		result := p(&data[0], size)
		assert(result == 1, loc = loc)
		accumulator += result
	}
	time.stopwatch_stop(&watch)
	timing = time.stopwatch_duration(watch)

	log.debug(accumulator)
	return
}

run_trial_size :: proc {
	run_trial_size_cmp,
	run_trial_size_zero,
}


bench_table :: proc(algo_name: string, plain, simd: $P) {
	string_buffer := strings.builder_make()
	defer strings.builder_destroy(&string_buffer)

	tbl: table.Table
	table.init(&tbl)
	defer table.destroy(&tbl)

	table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Iterations", "Scalar", "SIMD", "SIMD Relative (%)", "SIMD Relative (x)")

	for size in sizes {
		// Place the non-zero byte somewhere in the middle.
		needle_index := size / 2

		plain_timing := run_trial_size(plain, size, needle_index, RUNS_PER_SIZE)
		simd_timing  := run_trial_size(simd,  size, needle_index, RUNS_PER_SIZE)

		_plain := fmt.tprintf("%8M",  plain_timing)
		_simd  := fmt.tprintf("%8M",  simd_timing)
		_relp  := fmt.tprintf("%.3f %%", f64(simd_timing) / f64(plain_timing) * 100.0)
		_relx  := fmt.tprintf("%.3f x",  1 / (f64(simd_timing) / f64(plain_timing)))

		table.aligned_row_of_values(
			&tbl,
			.Right,
			algo_name,
			size, RUNS_PER_SIZE, _plain, _simd, _relp, _relx)
	}

	builder_writer := strings.to_writer(&string_buffer)

	fmt.sbprintln(&string_buffer)
	table.write_plain_table(builder_writer, &tbl)

	my_table_string := strings.to_string(string_buffer)
	log.info(my_table_string)
}

@test
benchmark_memory_procs :: proc(t: ^testing.T) {
	bench_table("memory_equal", plain_memory_equal, runtime.memory_equal)
	bench_table("memory_compare", plain_memory_compare, runtime.memory_compare)
	bench_table("memory_compare_zero", plain_memory_compare_zero, runtime.memory_compare_zero)
}