https://github.com/hymkor/go-bench-make-with-cap
benchmark between make with capacity vs append n-times
https://github.com/hymkor/go-bench-make-with-cap
benchmark go
Last synced: 24 days ago
JSON representation
benchmark between make with capacity vs append n-times
- Host: GitHub
- URL: https://github.com/hymkor/go-bench-make-with-cap
- Owner: hymkor
- License: mit
- Created: 2023-02-04T06:31:04.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-04T06:32:41.000Z (about 2 years ago)
- Last Synced: 2025-02-10T15:50:58.590Z (3 months ago)
- Topics: benchmark, go
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Report for benchmark between make with capacity vs append n-times
=================================================================1. `[]string{}` and `append` N-times
2. `make([]string,0,N)` and `append` N-times
3. `make([]string,N)` and no `append` (access with index)This repository also serves as your own template for how to take benchmarks
Source
------```go
package mainimport (
"fmt"
"testing"
)func BenchmarkAppendN(b *testing.B) {
buffer := []string{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buffer = append(buffer, fmt.Sprintf("%d", i))
}
}func BenchmarkMakeWithCapAndAppend(b *testing.B) {
buffer := make([]string, 0, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
buffer = append(buffer, fmt.Sprintf("%d", i))
}
}func BenchmarkNoAppend(b *testing.B) {
buffer := make([]string, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
buffer[i] = fmt.Sprintf("%d", i)
}
}
```Result
------```
go fmt
go test -bench . -benchmem
goos: windows
goarch: amd64
pkg: github.com/hymkor/go-bench-make-with-cap
cpu: Intel(R) Core(TM) i5-6500T CPU @ 2.50GHz
BenchmarkAppendN-4 5515822 237.3 ns/op 111 B/op 1 allocs/op
BenchmarkMakeWithCapAndAppend-4 9754629 125.8 ns/op 15 B/op 1 allocs/op
BenchmarkNoAppend-4 9762398 125.3 ns/op 15 B/op 1 allocs/op
PASS
ok github.com/hymkor/go-bench-make-with-cap 5.728s
```