• gconv
    • 基准性能测试

    gconv

    gf框架提供了非常强大的类型转换包gconv,可以实现将任何数据类型转换为指定的数据类型,对常用基本数据类型之间的无缝转换,同时也支持任意类型到struct对象的转换。由于gconv模块内部大量使用了断言而非反射(仅struct转换使用到了反射),因此执行的效率非常高。

    使用方式

    1. import "github.com/gogf/gf/g/util/gconv"

    接口文档

    https://godoc.org/github.com/gogf/gf/g/util/gconv

    1. // 基本类型
    2. func Bool(i interface{}) bool
    3. func Float32(i interface{}) float32
    4. func Float64(i interface{}) float64
    5. func Int(i interface{}) int
    6. func Int16(i interface{}) int16
    7. func Int32(i interface{}) int32
    8. func Int64(i interface{}) int64
    9. func Int8(i interface{}) int8
    10. func String(i interface{}) string
    11. func Uint(i interface{}) uint
    12. func Uint16(i interface{}) uint16
    13. func Uint32(i interface{}) uint32
    14. func Uint64(i interface{}) uint64
    15. func Uint8(i interface{}) uint8
    16. // slice类型
    17. func Bytes(i interface{}) []byte
    18. func Ints(i interface{}) []int
    19. func Floats(i interface{}) []float64
    20. func Strings(i interface{}) []string
    21. func Interfaces(i interface{}) []interface{}
    22. func Structs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
    23. func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
    24. // 时间类型
    25. func Time(i interface{}, format ...string) time.Time
    26. func TimeDuration(i interface{}) time.Duration
    27. // Map转换, 支持的类型包括:任意map或struct
    28. func Map(value interface{}, tags ...string) map[string]interface{}
    29. func MapDeep(value interface{}, tags ...string) map[string]interface{}
    30. // 对象转换
    31. func Struct(params interface{}, pointer interface{}, mapping ...map[string]string) error
    32. func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error
    33. // 根据类型名称执行基本类型转换(非struct转换)
    34. func Convert(i interface{}, t string, extraParams ...interface{}) interface{}

    基准性能测试

    测试转换变量值为123456789,类型int

    1. john@john-B85M:~/Workspace/Go/GOPATH/src/github.com/gogf/gf/g/util/gconv$ go test *.go -bench=".*" -benchmem
    2. goos: linux
    3. goarch: amd64
    4. BenchmarkString-4 20000000 71.8 ns/op 24 B/op 2 allocs/op
    5. BenchmarkInt-4 100000000 22.2 ns/op 8 B/op 1 allocs/op
    6. BenchmarkInt8-4 100000000 24.5 ns/op 8 B/op 1 allocs/op
    7. BenchmarkInt16-4 50000000 23.8 ns/op 8 B/op 1 allocs/op
    8. BenchmarkInt32-4 100000000 24.1 ns/op 8 B/op 1 allocs/op
    9. BenchmarkInt64-4 100000000 21.7 ns/op 8 B/op 1 allocs/op
    10. BenchmarkUint-4 100000000 22.2 ns/op 8 B/op 1 allocs/op
    11. BenchmarkUint8-4 50000000 25.6 ns/op 8 B/op 1 allocs/op
    12. BenchmarkUint16-4 50000000 32.1 ns/op 8 B/op 1 allocs/op
    13. BenchmarkUint32-4 50000000 27.7 ns/op 8 B/op 1 allocs/op
    14. BenchmarkUint64-4 50000000 28.1 ns/op 8 B/op 1 allocs/op
    15. BenchmarkFloat32-4 10000000 155 ns/op 24 B/op 2 allocs/op
    16. BenchmarkFloat64-4 10000000 177 ns/op 24 B/op 2 allocs/op
    17. BenchmarkTime-4 5000000 240 ns/op 72 B/op 4 allocs/op
    18. BenchmarkTimeDuration-4 50000000 26.2 ns/op 8 B/op 1 allocs/op
    19. BenchmarkBytes-4 10000000 149 ns/op 128 B/op 3 allocs/op
    20. BenchmarkStrings-4 10000000 223 ns/op 40 B/op 3 allocs/op
    21. BenchmarkInts-4 20000000 55.0 ns/op 16 B/op 2 allocs/op
    22. BenchmarkFloats-4 10000000 186 ns/op 32 B/op 3 allocs/op
    23. BenchmarkInterfaces-4 20000000 66.6 ns/op 24 B/op 2 allocs/op
    24. PASS
    25. ok command-line-arguments 35.356s