Skip to content

Instantly share code, notes, and snippets.

@iamgoangle
Created May 28, 2019 18:29
Show Gist options
  • Select an option

  • Save iamgoangle/0f2c32febbfe345f1045737c855b9ea7 to your computer and use it in GitHub Desktop.

Select an option

Save iamgoangle/0f2c32febbfe345f1045737c855b9ea7 to your computer and use it in GitHub Desktop.
Locality of reference
package main
import (
"fmt"
"time"
)
func A() {
start := time.Now()
var acc int = 0
var myArr [20000][20000]int
for col := 0; col < 20000; col++ {
for row := 0; row < 20000; row++ {
acc += myArr[row][col]
}
}
end := time.Since(start)
fmt.Println(end)
fmt.Println(acc)
}
func B() {
start := time.Now()
var acc int
var myArr [20000][20000]int
for row := 0; row < 20000; row++ {
for col := 0; col < 20000; col++ {
acc += myArr[row][col]
}
}
end := time.Since(start)
fmt.Println(end)
fmt.Println(acc)
}
func main() {
// why???? :)
A() // 8-9 sec
B() // 1-2 sec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment