Skip to content

Instantly share code, notes, and snippets.

@0xc0d
Created September 7, 2021 12:36
Show Gist options
  • Save 0xc0d/9873c5385435f50f3f3cac998453c376 to your computer and use it in GitHub Desktop.
Save 0xc0d/9873c5385435f50f3f3cac998453c376 to your computer and use it in GitHub Desktop.
CPU Cache Line vs Memory Speed
package main
import (
"fmt"
"time"
)
type BigStruct struct {
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
_ int64
}
func main() {
a := make([][]BigStruct, 1e4)
for i := range a {
a[i] = make([]BigStruct, 1e4)
}
s := time.Now()
var m BigStruct
for i := 0; i < len(a); i++ {
for j := len(a[i]); j < len(a[i]); j++ {
m = a[i][j]
}
}
fmt.Println(time.Since(s))
s = time.Now()
for i := 0; i < len(a); i++ {
for j := len(a[i]); j < len(a[i]); j++ {
m = a[j][i]
}
}
fmt.Println(time.Since(s))
// just to fool compiler
func(m BigStruct) {}(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment