Created
April 12, 2016 22:02
-
-
Save wcharczuk/85c5aa0417039b295a5e5a014a69e74d to your computer and use it in GitHub Desktop.
Pointers vs. Structs
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" | |
| ) | |
| const ( | |
| GraphSize = 8 | |
| RecursionDepth = 5 | |
| RecursionWidth = 5 | |
| Iterations = 100 | |
| ) | |
| type SubGraph interface{} | |
| type MyGraph struct { | |
| SG1 SubGraph | |
| SG2 SubGraph | |
| SG3 SubGraph | |
| SG4 SubGraph | |
| I1 int64 | |
| I2 int64 | |
| I3 int64 | |
| I4 int64 | |
| I5 int64 | |
| I6 int64 | |
| I7 int64 | |
| I8 int64 | |
| } | |
| func NewGraph() SubGraph { | |
| return newSubGraph(GraphSize) | |
| } | |
| func newSubGraph(n int) SubGraph { | |
| if n == 0 { | |
| return MyGraph{ | |
| I1: 1, | |
| I2: 2, | |
| I3: 3, | |
| I4: 4, | |
| I5: 5, | |
| I6: 6, | |
| I7: 7, | |
| I8: 8, | |
| } | |
| } | |
| return MyGraph{ | |
| SG1: newSubGraph(n - 1), | |
| SG2: newSubGraph(n - 1), | |
| SG3: newSubGraph(n - 1), | |
| SG4: newSubGraph(n - 1), | |
| I1: 1, | |
| I2: 2, | |
| I3: 3, | |
| I4: 4, | |
| I5: 5, | |
| I6: 6, | |
| I7: 7, | |
| I8: 8, | |
| } | |
| } | |
| func NewPtrGraph() SubGraph { | |
| return newPtrSubGraph(GraphSize) | |
| } | |
| func newPtrSubGraph(n int) SubGraph { | |
| if n == 0 { | |
| return &MyGraph{ | |
| I1: 1, | |
| I2: 2, | |
| I3: 3, | |
| I4: 4, | |
| I5: 5, | |
| I6: 6, | |
| I7: 7, | |
| I8: 8, | |
| } | |
| } | |
| return &MyGraph{ | |
| SG1: newPtrSubGraph(n - 1), | |
| SG2: newPtrSubGraph(n - 1), | |
| SG3: newPtrSubGraph(n - 1), | |
| SG4: newPtrSubGraph(n - 1), | |
| I1: 1, | |
| I2: 2, | |
| I3: 3, | |
| I4: 4, | |
| I5: 5, | |
| I6: 6, | |
| I7: 7, | |
| I8: 8, | |
| } | |
| } | |
| func doThingsWith(g SubGraph, n int) bool { | |
| if n == 0 { | |
| return true | |
| } | |
| result := true | |
| for x := 0; x < RecursionWidth; x++ { | |
| result = result && doThingsWith(g, n-1) | |
| } | |
| return result | |
| } | |
| func main() { | |
| start := time.Now() | |
| for x := 0; x < Iterations; x++ { | |
| doThingsWith(NewGraph(), RecursionDepth) | |
| } | |
| end := time.Now() | |
| fmt.Printf("Non-Pointer: %v\n", end.Sub(start)) | |
| start = time.Now() | |
| for x := 0; x < Iterations; x++ { | |
| doThingsWith(NewPtrGraph(), RecursionDepth) | |
| } | |
| end = time.Now() | |
| fmt.Printf("Pointer: %v\n", end.Sub(start)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment