Created
April 17, 2023 19:10
-
-
Save mateothegreat/d9861a63a57491803c5cea93b05d93df to your computer and use it in GitHub Desktop.
Slices vs. Array performance in golang.
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 "testing" | |
var gs = make([]byte, 1000) | |
var ga [1000]byte | |
func BenchmarkSliceGlobal(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for j, v := range gs { | |
gs[j]++ | |
gs[j] = gs[j] + v + 10 | |
gs[j] += v | |
} | |
} | |
} | |
func BenchmarkArrayGlobal(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for j, v := range ga { | |
ga[j]++ | |
ga[j] = ga[j] + v + 10 | |
ga[j] += v | |
} | |
} | |
} | |
func BenchmarkSliceLocal(b *testing.B) { | |
var s = make([]byte, 1000) | |
for i := 0; i < b.N; i++ { | |
for j, v := range s { | |
s[j]++ | |
s[j] = s[j] + v + 10 | |
s[j] += v | |
} | |
} | |
} | |
func BenchmarkArrayLocal(b *testing.B) { | |
var a [1000]byte | |
for i := 0; i < b.N; i++ { | |
for j, v := range a { | |
a[j]++ | |
a[j] = a[j] + v + 10 | |
a[j] += v | |
} | |
} | |
} |
Author
mateothegreat
commented
Apr 17, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment