Created
September 7, 2021 12:36
-
-
Save 0xc0d/9873c5385435f50f3f3cac998453c376 to your computer and use it in GitHub Desktop.
CPU Cache Line vs Memory Speed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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