File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ package Text
2+
3+ import (
4+ "strings"
5+ "testing"
6+ )
7+
8+ const text = "Hello dear World"
9+ const count = 25
10+
11+ var result string
12+
13+ func BenchmarkStringBuilderConcat (b * testing.B ) {
14+ var r string
15+ for n := 0 ; n < b .N ; n ++ {
16+ r = benchmarkStringBuilderConcat (text , count )
17+ }
18+ result = r
19+ }
20+
21+ func BenchmarkStringConcat (b * testing.B ) {
22+ var r string
23+ for n := 0 ; n < b .N ; n ++ {
24+ r = benchmarkStringConcat (text , count )
25+ }
26+ result = r
27+ }
28+
29+ func BenchmarkGoStringBuilderConcat (b * testing.B ) {
30+ var r string
31+ for n := 0 ; n < b .N ; n ++ {
32+ r = benchmarkGoStringBuilderConcat (text , count )
33+ }
34+ result = r
35+ }
36+
37+ func benchmarkStringBuilderConcat (text string , count int ) string {
38+ s := & StringBuilder {}
39+ for i := 0 ; i < count ; i ++ {
40+ s .Append (text )
41+ }
42+
43+ return s .ToString ()
44+ }
45+
46+ func benchmarkStringConcat (text string , count int ) string {
47+ s := ""
48+ for i := 0 ; i < count ; i ++ {
49+ s += text
50+ }
51+
52+ return s
53+ }
54+
55+ func benchmarkGoStringBuilderConcat (text string , count int ) string {
56+ s := & strings.Builder {}
57+
58+ for i := 0 ; i < count ; i ++ {
59+ s .WriteString (text )
60+ }
61+
62+ return s .String ()
63+ }
You can’t perform that action at this time.
0 commit comments