123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package main
- // #cgo CFLAGS: -O3
- // #cgo LDFLAGS: -O3
- import (
- "C"
- "hexasoftware/lib/vnode"
- "strings"
- )
- import (
- "hexasoftware/lib/bench"
- "reflect"
- )
- func NCastFunc(fn interface{}) func(...interface{}) interface{} {
- fnRefl := reflect.ValueOf(fn)
- ret := func(args ...interface{}) interface{} {
- nargs := []reflect.Value{}
- for _, a := range args {
- nargs = append(nargs, reflect.ValueOf(a))
- }
- return fnRefl.Call(nargs)[0].Interface()
- }
- return ret
- }
- var srsplitNode = vnode.New(NCastFunc(strings.Fields), 1)
- var srjoinNode = vnode.New(NCastFunc(strings.Join), 2)
- var rsplitNode = vnode.New(strings.Fields)
- var rjoinNode = vnode.New(strings.Join)
- var splitNode = vnode.New(func(args ...interface{}) interface{} {
- return strings.Fields(args[0].(string))
- }, 1) // Inform number of inputs
- var joinNode = vnode.New(func(args ...interface{}) interface{} {
- return strings.Join(args[0].([]string), args[1].(string))
- }, 2)
- func main() {
- s := "This is a test"
- splitNode.Source(s)
- joinNode.Source(splitNode, ",")
- rsplitNode.Source(s)
- rjoinNode.Source(rsplitNode, ",")
- srsplitNode.Source(s)
- srjoinNode.Source(srsplitNode, ",")
- // Exec full strenght
- bench.Exec("node iface", func() interface{} {
- return joinNode.Exec()
- })
- bench.Exec("node refl", func() interface{} {
- return rjoinNode.Exec()
- })
- bench.Exec("node Smart refl", func() interface{} {
- return srjoinNode.Exec()
- })
- bench.Exec("node native", func() interface{} {
- return strings.Join(strings.Fields(s), ",")
- })
- }
|