-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #663 from devlights:add-speeddiff-append-string-to…
…-byte-slice Add speeds for setting strings to byteslice
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
examples/singleapp/speed_diff_append_string_to_byteslice/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# これは何? | ||
|
||
バイトスライスに対して | ||
|
||
- fmt.Sprintf | ||
- fmt.Appendf | ||
- 直接appendで追加していく | ||
|
||
のどれが最も速いのかをベンチマークしてみたものです。 | ||
|
||
[slogのハンドラ作成ガイドドキュメント](https://github.com/golang/example/blob/master/slog-handler-guide/README.md#speed)に記載があったので試してみました。 | ||
|
||
以下、Gitpod上で試してみた結果です。 | ||
|
||
```sh | ||
$ task | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/devlights/try-golang/examples/singleapp/speed_diff_append_string_to_byteslice | ||
cpu: AMD EPYC 7B13 | ||
BenchmarkUseFmtSprintf-16 3160230 320.0 ns/op | ||
BenchmarkUseFmtAppendf-16 9861033 133.7 ns/op | ||
BenchmarkUseDirectAppend-16 21009861 83.87 ns/op | ||
PASS | ||
ok github.com/devlights/try-golang/examples/singleapp/speed_diff_append_string_to_byteslice 7.914s | ||
``` | ||
|
||
処理は冗長になってしまいますが、直接appendが最も速いです。 | ||
|
||
## SeeAlso | ||
|
||
- https://gist.github.com/devlights/ffd22f78297a563c9bebcb9a9baa7f5f |
9 changes: 9 additions & 0 deletions
9
examples/singleapp/speed_diff_append_string_to_byteslice/Taskfile.yml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# https://taskfile.dev | ||
|
||
version: '3' | ||
|
||
tasks: | ||
default: | ||
cmds: | ||
- go test -bench . | ||
silent: true |
40 changes: 40 additions & 0 deletions
40
examples/singleapp/speed_diff_append_string_to_byteslice/main_test.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func BenchmarkUseFmtSprintf(b *testing.B) { | ||
b.StopTimer() | ||
buf := make([]byte, 0, 1024*1024*500) | ||
b.StartTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
s := fmt.Sprintf("%s:%d\n", "key", i) | ||
buf = append(buf, s...) //lint:ignore SA4010 It's ok. | ||
} | ||
} | ||
|
||
func BenchmarkUseFmtAppendf(b *testing.B) { | ||
b.StopTimer() | ||
buf := make([]byte, 0, 1024*1024*500) | ||
b.StartTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
buf = fmt.Appendf(buf, "%s:%d\n", "key", i) | ||
} | ||
} | ||
|
||
func BenchmarkUseDirectAppend(b *testing.B) { | ||
b.StopTimer() | ||
buf := make([]byte, 0, 1024*1024*500) | ||
b.StartTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
buf = append(buf, "key:"...) //lint:ignore SA4010 It's ok. | ||
buf = append(buf, strconv.Itoa(i)...) //lint:ignore SA4010 It's ok. | ||
buf = append(buf, '\n') //lint:ignore SA4010 It's ok. | ||
} | ||
} |