Skip to content

Commit f0fbe82

Browse files
authored
Merge pull request #564 from devlights/add-gocollective-example
2 parents 7e8ed2f + 92745df commit f0fbe82

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
3+
tasks:
4+
run:
5+
cmds:
6+
- go run -race main.go
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Stackoverflow Go Collective example
2+
//
3+
// How to efficiently concatenate strings in go
4+
//
5+
// URL
6+
// - https://stackoverflow.com/questions/1760757/how-to-efficiently-concatenate-strings-in-go
7+
//
8+
// REFERENCES
9+
// - https://pkg.go.dev/strings@latest#Builder
10+
// - https://yourbasic.org/golang/measure-execution-time/
11+
package main
12+
13+
import (
14+
"bytes"
15+
"log"
16+
"strings"
17+
"time"
18+
19+
"github.com/devlights/gomy/times"
20+
)
21+
22+
func init() {
23+
log.SetFlags(0)
24+
}
25+
26+
func main() {
27+
// Go 1.10 より strings.Builder が追加された。
28+
// bytes.Buffer でも良いが、文字列の場合はこちらのほうが効率が良い。
29+
30+
var (
31+
sb strings.Builder
32+
buf bytes.Buffer
33+
)
34+
35+
// ----- bytes.Buffer ----- //
36+
elapsed := times.Stopwatch(func(start time.Time) {
37+
for i := 0; i < 3_000_000; i++ {
38+
buf.WriteString("i")
39+
}
40+
})
41+
log.Printf("[bytes.Buffer ] len=%d, elapsed=%v", buf.Len(), elapsed)
42+
43+
// ----- strings.Builder ----- //
44+
elapsed = times.Stopwatch(func(start time.Time) {
45+
for i := 0; i < 3_000_000; i++ {
46+
sb.WriteString("i")
47+
}
48+
})
49+
log.Printf("[strings.Builder] len=%d, elapsed=%v", sb.Len(), elapsed)
50+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devlights/try-golang
33
go 1.18
44

55
require (
6-
github.com/devlights/gomy v0.4.6
6+
github.com/devlights/gomy v0.4.8
77
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
88
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
99
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/devlights/gomy v0.4.6 h1:S/UiHgNF1/dF/MRhxgOT1Bsd5i33/Ly9qOSePU3Q7KU=
2-
github.com/devlights/gomy v0.4.6/go.mod h1:s6/L0jEn7J/F1bIRNu3nkf+Lz/n24RClTxD76DhqGEU=
1+
github.com/devlights/gomy v0.4.8 h1:LLWJssPBc/ZG29iPRxzPep5dJvSCcc1zkue4LFaLhuE=
2+
github.com/devlights/gomy v0.4.8/go.mod h1:s6/L0jEn7J/F1bIRNu3nkf+Lz/n24RClTxD76DhqGEU=
33
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
44
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
55
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=

0 commit comments

Comments
 (0)