-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9cb194d
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
go test -v -bench . |
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
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]> |
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
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)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bench | ||
|
||
type FloatCol struct { | ||
rows []float64 | ||
} |
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
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 | ||
} |
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
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 | ||
} |