aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/gob/timing_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2014-10-17 09:00:07 -0700
committerRob Pike <r@golang.org>2014-10-17 09:00:07 -0700
commit5e713062b42110b9f7ccd1c326fab0e42b5b8c35 (patch)
tree9eb8e99257366521e292fb30727531579e0904e0 /src/encoding/gob/timing_test.go
parentfb173c41850c9ad049098be657c923589329fab8 (diff)
downloadgo-5e713062b42110b9f7ccd1c326fab0e42b5b8c35.tar.gz
go-5e713062b42110b9f7ccd1c326fab0e42b5b8c35.zip
encoding/gob: speed up encoding of arrays and slices
We borrow a trick from the fmt package and avoid reflection to walk the elements when possible. We could push further with unsafe (and we may) but this is a good start. Decode can benefit similarly; it will be done separately. Use go generate (engen.go) to produce the helper functions (enc_helpers.go). benchmark old ns/op new ns/op delta BenchmarkEndToEndPipe 6593 6482 -1.68% BenchmarkEndToEndByteBuffer 3662 3684 +0.60% BenchmarkEndToEndSliceByteBuffer 350306 351693 +0.40% BenchmarkComplex128Slice 96347 80045 -16.92% BenchmarkInt32Slice 42484 26008 -38.78% BenchmarkFloat64Slice 51143 36265 -29.09% BenchmarkStringSlice 53402 35077 -34.32% LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/156310043
Diffstat (limited to 'src/encoding/gob/timing_test.go')
-rw-r--r--src/encoding/gob/timing_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/encoding/gob/timing_test.go b/src/encoding/gob/timing_test.go
index ec55c4d63d..abfe936e83 100644
--- a/src/encoding/gob/timing_test.go
+++ b/src/encoding/gob/timing_test.go
@@ -131,3 +131,67 @@ func TestCountDecodeMallocs(t *testing.T) {
t.Fatalf("mallocs per decode of type Bench: %v; wanted 4\n", allocs)
}
}
+
+func BenchmarkComplex128Slice(b *testing.B) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ a := make([]complex128, 1000)
+ for i := range a {
+ a[i] = 1.2 + 3.4i
+ }
+ for i := 0; i < b.N; i++ {
+ buf.Reset()
+ err := enc.Encode(a)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+func BenchmarkInt32Slice(b *testing.B) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ a := make([]int32, 1000)
+ for i := range a {
+ a[i] = 1234
+ }
+ for i := 0; i < b.N; i++ {
+ buf.Reset()
+ err := enc.Encode(a)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+func BenchmarkFloat64Slice(b *testing.B) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ a := make([]float64, 1000)
+ for i := range a {
+ a[i] = 1.23e4
+ }
+ for i := 0; i < b.N; i++ {
+ buf.Reset()
+ err := enc.Encode(a)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+func BenchmarkStringSlice(b *testing.B) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ a := make([]string, 1000)
+ for i := range a {
+ a[i] = "now is the time"
+ }
+ for i := 0; i < b.N; i++ {
+ buf.Reset()
+ err := enc.Encode(a)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}