forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching.go
67 lines (52 loc) · 1.32 KB
/
caching.go
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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// http://play.golang.org/p/GQQXh3cf15
// package caching provides code to show why CPU caches matter and the way
// the hardware caches memory affects performance.
package caching
import "fmt"
const cols = 64
// Set the size of each row to be 4k.
//const rows = 4 * 1024
// Set the size of each row to be 64k.
const rows = 64 * 1024
// matrix represents a set of columns that each exist on
// their own cache line.
var matrix [cols][rows]byte
// init sets ~13% of the matrix to 0XFF.
func init() {
var ctr int
for col := 0; col < cols; col++ {
for row := 0; row < rows; row++ {
if row%8 == 0 {
matrix[col][row] = 0xFF
ctr++
}
}
}
fmt.Println(ctr, "Elements set out of", cols*rows)
}
// rowTraverse traverses the matrix linearly by each column for each row.
func rowTraverse() int {
var ctr int
for col := 0; col < cols; col++ {
for row := 0; row < rows; row++ {
if matrix[col][row] == 0xFF {
ctr++
}
}
}
return ctr
}
// colTraverse traverses the matrix linearly by each row for each column.
func colTraverse() int {
var ctr int
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
if matrix[col][row] == 0xFF {
ctr++
}
}
}
return ctr
}