Created
November 26, 2018 06:28
-
-
Save ivancorrales/99b8c8dc61698a3f03e163532cd774e9 to your computer and use it in GitHub Desktop.
Benchmark test to demonstrate loc doesn't means performance
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 loc | |
import ( | |
"os" | |
"testing" | |
) | |
const maxElements = 1000 | |
var elements = make([]int, maxElements) | |
func TestMain(m *testing.M) { | |
for i := 0; i < maxElements; i++ { | |
elements[i] = i | |
} | |
os.Exit(m.Run()) | |
} | |
func Add(items []int, item int) []int { | |
return append(items, item) | |
} | |
func Add2(items []int, item int) []int { | |
n := len(items) | |
output := make([]int, n+1) | |
output[n] = item | |
copy(items[0:n], output) | |
return output | |
} | |
func BenchmarkAdd(b *testing.B) { | |
b.ReportAllocs() | |
var result []int | |
for i := 0; i < b.N; i++ { | |
b.StartTimer() | |
result = Add(elements, 10) | |
} | |
if len(result) != maxElements+1 { | |
b.Fatalf("Invalid number of elements, expected %d but retrieved %d", maxElements+1, len(result)) | |
} | |
} | |
func BenchmarkAdd2(b *testing.B) { | |
b.ReportAllocs() | |
var result []int | |
for i := 0; i < b.N; i++ { | |
b.StartTimer() | |
result = Add2(elements, 10) | |
} | |
if len(result) != maxElements+1 { | |
b.Fatalf("Invalid number of elements, expected %d but retrieved %d", maxElements+1, len(result)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go test -bench=. -benchmem -memprofile memprofile.out -cpuprofile profile.out