Created
January 6, 2015 07:26
-
-
Save fujiwara/4850a936f87cedb021ff to your computer and use it in GitHub Desktop.
toInt64 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
package fluent_test | |
import ( | |
"reflect" | |
"testing" | |
) | |
var testNumbers = []interface{}{ | |
int(10), | |
uint(10), | |
int32(10), | |
uint32(10), | |
int64(10), | |
uint64(10), | |
float32(10.123), | |
float32(10.234), | |
} | |
func TestToInt64(t *testing.T) { | |
for _, n := range testNumbers { | |
if toInt64(n) != int64(10) { | |
t.Errorf("%s can't convert to int64(10)", n) | |
} | |
if toInt64ReflectConvert(n) != int64(10) { | |
t.Errorf("%s can't convert to int64(10)", n) | |
} | |
if toInt64If(n) != int64(10) { | |
t.Errorf("%s can't convert to int64(10)", n) | |
} | |
} | |
} | |
func toInt64(v interface{}) int64 { | |
switch v.(type) { | |
case int, int32, int64: | |
return int64(reflect.ValueOf(v).Int()) | |
case uint, uint32, uint64: | |
return int64(reflect.ValueOf(v).Uint()) | |
case float32, float64: | |
return int64(reflect.ValueOf(v).Float()) | |
default: | |
return int64(0) | |
} | |
} | |
func toInt64ReflectConvert(v interface{}) int64 { | |
var i int64 | |
switch v.(type) { | |
case int, uint, int64, uint64, int32, uint32, float32, float64: | |
value := reflect.ValueOf(v) | |
v2 := value.Convert(reflect.TypeOf(i)) | |
i = v2.Int() | |
} | |
return i | |
} | |
func toInt64If(v interface{}) int64 { | |
var i int64 | |
switch v.(type) { | |
case int, uint, int64, uint64, int32, uint32, float32, float64: | |
if value, ok := v.(int64); ok { | |
i = value | |
} else if value, ok := v.(int); ok { | |
i = int64(value) | |
} else if value, ok := v.(uint); ok { | |
i = int64(value) | |
} else if value, ok := v.(uint64); ok { | |
i = int64(value) | |
} else if value, ok := v.(int32); ok { | |
i = int64(value) | |
} else if value, ok := v.(uint32); ok { | |
i = int64(value) | |
} else if value, ok := v.(float32); ok { | |
i = int64(value) | |
} else if value, ok := v.(float64); ok { | |
i = int64(value) | |
} | |
} | |
return i | |
} | |
func BenchmarkToInt64(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, n := range testNumbers { | |
_ = toInt64(n) | |
} | |
} | |
} | |
func BenchmarkToInt64ReflectConvert(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, n := range testNumbers { | |
_ = toInt64ReflectConvert(n) | |
} | |
} | |
} | |
func BenchmarkToInt64If(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, n := range testNumbers { | |
_ = toInt64If(n) | |
} | |
} | |
} |
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
$ go test -bench ToInt -benchmem | |
PASS | |
BenchmarkToInt64 10000000 207 ns/op 0 B/op 0 allocs/op | |
BenchmarkToInt64ReflectConvert 1000000 1201 ns/op 128 B/op 16 allocs/op | |
BenchmarkToInt64If 3000000 436 ns/op 0 B/op 0 allocs/op | |
ok github.com/fujiwara/fluent-agent-hydra/fluent 5.288s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment