Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
hgfischer committed Jun 21, 2014
0 parents commit 9cb194d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
go test -v -bench .
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Introduction

Naive performance test of doing type assertion using:

* The native way

or...

* Using reflection to avoid code repetition

# Results

```
BenchmarkAppendNativeTypeAssertion 5000000 666 ns/op
BenchmarkAppendReflectionTypeAssertion 5000000 529 ns/op
```

# Thanks to

* Dan Kortschak <[email protected]>
* Dmitry Vyukov <[email protected]>
* Xingtao Zhao <[email protected]>
* Egon <[email protected]>
* Chris Dollin <[email protected]>
21 changes: 21 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package bench

import "testing"

func BenchmarkAppendNativeTypeAssertion(b *testing.B) {
for i := 0; i < b.N; i++ {
col := &FloatCol{}
col.AppendNativeTypeAssertion(1.2)
col.AppendNativeTypeAssertion("4.3")
col.AppendNativeTypeAssertion(float32(123))
}
}

func BenchmarkAppendReflectionTypeAssertion(b *testing.B) {
for i := 0; i < b.N; i++ {
col := &FloatCol{}
col.AppendReflectionTypeAssertion(1.2)
col.AppendReflectionTypeAssertion("4.3")
col.AppendReflectionTypeAssertion(float32(123))
}
}
5 changes: 5 additions & 0 deletions floatcol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bench

type FloatCol struct {
rows []float64
}
42 changes: 42 additions & 0 deletions native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bench

import (
"fmt"
"strconv"
)

func (c *FloatCol) AppendNativeTypeAssertion(row interface{}) error {
switch value := row.(type) {
case float32:
c.rows = append(c.rows, float64(value))
case float64:
c.rows = append(c.rows, float64(value))
case int:
c.rows = append(c.rows, float64(value))
case int8:
c.rows = append(c.rows, float64(value))
case int16:
c.rows = append(c.rows, float64(value))
case int32:
c.rows = append(c.rows, float64(value))
case int64:
c.rows = append(c.rows, float64(value))
case uint:
c.rows = append(c.rows, float64(value))
case uint8:
c.rows = append(c.rows, float64(value))
case uint16:
c.rows = append(c.rows, float64(value))
case uint32:
c.rows = append(c.rows, float64(value))
case uint64:
c.rows = append(c.rows, float64(value))
default:
num, err := strconv.ParseFloat(fmt.Sprint(row), 64)
if err != nil {
return err
}
c.rows = append(c.rows, num)
}
return nil
}
25 changes: 25 additions & 0 deletions reflection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package bench

import (
"reflect"
"strconv"
)

func (c *FloatCol) AppendReflectionTypeAssertion(row interface{}) error {
rv := reflect.ValueOf(row)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Uintptr:
c.rows = append(c.rows, float64(rv.Int()))
case reflect.Float32, reflect.Float64:
c.rows = append(c.rows, rv.Float())
default:
num, err := strconv.ParseFloat(rv.String(), 64)
if err != nil {
return err
}
c.rows = append(c.rows, num)
}
return nil
}

0 comments on commit 9cb194d

Please sign in to comment.