Skip to content

Commit

Permalink
strings: add benchmark for Builder.WriteString
Browse files Browse the repository at this point in the history
This is in preparation for an optimization.

```
name                                     time/op
BuildString_WriteString/1Write_NoGrow-8  30.8ns ± 0%
BuildString_WriteString/3Write_NoGrow-8   111ns ± 0%
BuildString_WriteString/3Write_Grow-8    44.1ns ± 4%

name                                     alloc/op
BuildString_WriteString/1Write_NoGrow-8   48.0B ± 0%
BuildString_WriteString/3Write_NoGrow-8    336B ± 0%
BuildString_WriteString/3Write_Grow-8      112B ± 0%

name                                     allocs/op
BuildString_WriteString/1Write_NoGrow-8    1.00 ± 0%
BuildString_WriteString/3Write_NoGrow-8    3.00 ± 0%
BuildString_WriteString/3Write_Grow-8      1.00 ± 0%
```

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I0e2c82edcdc72f381c5160f315401678ff76f3d9
Reviewed-on: https://go-review.googlesource.com/c/go/+/507777
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
rogpeppe authored and gopherbot committed Jul 20, 2023
1 parent 47b403a commit 1e576ef
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/strings/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,22 @@ func BenchmarkBuildString_Builder(b *testing.B) {
})
}

func BenchmarkBuildString_WriteString(b *testing.B) {
someString := string(someBytes)
benchmarkBuilder(b, func(b *testing.B, numWrite int, grow bool) {
for i := 0; i < b.N; i++ {
var buf Builder
if grow {
buf.Grow(len(someString) * numWrite)
}
for i := 0; i < numWrite; i++ {
buf.WriteString(someString)
}
sinkS = buf.String()
}
})
}

func BenchmarkBuildString_ByteBuffer(b *testing.B) {
benchmarkBuilder(b, func(b *testing.B, numWrite int, grow bool) {
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 1e576ef

Please sign in to comment.