Last active
June 26, 2022 21:29
-
-
Save vodolaz095/5d4de4dfde089e298dc36edc6bd7ad54 to your computer and use it in GitHub Desktop.
golang benchmark of string concatenation
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
package strings | |
import ( | |
"bytes" | |
"fmt" | |
"os" | |
"strings" | |
"testing" | |
) | |
func BenchmarkPrintf(b *testing.B) { | |
var err error | |
sink, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) | |
if err != nil { | |
b.Fatalf("%s : while opening /dev/null", err) | |
} | |
for i := 0; i < b.N; i++ { | |
_, err = fmt.Fprintf(sink, "%s %s", "first", "second") | |
if err != nil { | |
b.Fatalf("%s : while writing to /dev/null", err) | |
} | |
} | |
err = sink.Close() | |
if err != nil { | |
b.Fatalf("%s : while closing /dev/null", err) | |
} | |
} | |
func BenchmarkConcat(b *testing.B) { | |
var err error | |
sink, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) | |
if err != nil { | |
b.Fatalf("%s : while opening /dev/null", err) | |
} | |
for i := 0; i < b.N; i++ { | |
var result = "a" + " " + "b" | |
_, err = fmt.Fprint(sink, result) | |
if err != nil { | |
b.Fatalf("%s : while writing to /dev/null", err) | |
} | |
} | |
err = sink.Close() | |
if err != nil { | |
b.Fatalf("%s : while closing /dev/null", err) | |
} | |
} | |
func BenchmarkBufferFresh(b *testing.B) { | |
var err error | |
sink, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) | |
if err != nil { | |
b.Fatalf("%s : while opening dev/null", err) | |
} | |
for i := 0; i < b.N; i++ { | |
buf := bytes.NewBufferString("") | |
buf.WriteString("a") | |
buf.WriteString(" ") | |
buf.WriteString("b") | |
_, err = sink.Write(buf.Bytes()) | |
if err != nil { | |
b.Fatalf("%s : while writing to /dev/null", err) | |
} | |
} | |
err = sink.Close() | |
if err != nil { | |
b.Fatalf("%s : while closing /dev/null", err) | |
} | |
} | |
func BenchmarkBufferReuse(b *testing.B) { | |
var err error | |
sink, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) | |
if err != nil { | |
b.Fatalf("%s : while opening dev/null", err) | |
} | |
buf := bytes.NewBufferString("") | |
for i := 0; i < b.N; i++ { | |
buf.WriteString("a") | |
buf.WriteString(" ") | |
buf.WriteString("b") | |
_, err = sink.Write(buf.Bytes()) | |
if err != nil { | |
b.Fatalf("%s : while writing to /dev/null", err) | |
} | |
buf.Truncate(0) | |
} | |
err = sink.Close() | |
if err != nil { | |
b.Fatalf("%s : while closing /dev/null", err) | |
} | |
} | |
func BenchmarkBuilder(b *testing.B) { | |
var err error | |
sink, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) | |
if err != nil { | |
b.Fatalf("%s : while opening dev/null", err) | |
} | |
var buf strings.Builder | |
for i := 0; i < b.N; i++ { | |
buf.WriteString("a") | |
buf.WriteString(" ") | |
buf.WriteString("b") | |
_, err = sink.WriteString(buf.String()) | |
if err != nil { | |
b.Fatalf("%s : while writing to /dev/null", err) | |
} | |
buf.Reset() | |
} | |
err = sink.Close() | |
if err != nil { | |
b.Fatalf("%s : while closing /dev/null", err) | |
} | |
} | |
/* | |
[vodolaz095@steel strings]$ go test -bench=. strings_test.go | |
goos: linux | |
goarch: amd64 | |
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz | |
BenchmarkPrintf-8 1768626 675.9 ns/op | |
BenchmarkConcat-8 1760847 681.7 ns/op | |
BenchmarkBufferFresh-8 1978248 606.8 ns/op | |
BenchmarkBufferReuse-8 2003402 601.7 ns/op | |
BenchmarkBuilder-8 1930663 623.0 ns/op | |
PASS | |
ok command-line-arguments 9.234s | |
//*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment