Created
May 28, 2019 18:29
-
-
Save iamgoangle/0f2c32febbfe345f1045737c855b9ea7 to your computer and use it in GitHub Desktop.
Locality of reference
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" | |
| ) | |
| 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