main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. // #cgo CFLAGS: -O3
  3. // #cgo LDFLAGS: -O3
  4. import (
  5. "C"
  6. "hexasoftware/lib/vnode"
  7. "strings"
  8. )
  9. import (
  10. "hexasoftware/lib/bench"
  11. "reflect"
  12. )
  13. func NCastFunc(fn interface{}) func(...interface{}) interface{} {
  14. fnRefl := reflect.ValueOf(fn)
  15. ret := func(args ...interface{}) interface{} {
  16. nargs := []reflect.Value{}
  17. for _, a := range args {
  18. nargs = append(nargs, reflect.ValueOf(a))
  19. }
  20. return fnRefl.Call(nargs)[0].Interface()
  21. }
  22. return ret
  23. }
  24. var srsplitNode = vnode.New(NCastFunc(strings.Fields), 1)
  25. var srjoinNode = vnode.New(NCastFunc(strings.Join), 2)
  26. var rsplitNode = vnode.New(strings.Fields)
  27. var rjoinNode = vnode.New(strings.Join)
  28. var splitNode = vnode.New(func(args ...interface{}) interface{} {
  29. return strings.Fields(args[0].(string))
  30. }, 1) // Inform number of inputs
  31. var joinNode = vnode.New(func(args ...interface{}) interface{} {
  32. return strings.Join(args[0].([]string), args[1].(string))
  33. }, 2)
  34. func main() {
  35. s := "This is a test"
  36. splitNode.Source(s)
  37. joinNode.Source(splitNode, ",")
  38. rsplitNode.Source(s)
  39. rjoinNode.Source(rsplitNode, ",")
  40. srsplitNode.Source(s)
  41. srjoinNode.Source(srsplitNode, ",")
  42. // Exec full strenght
  43. bench.Exec("node iface", func() interface{} {
  44. return joinNode.Exec()
  45. })
  46. bench.Exec("node refl", func() interface{} {
  47. return rjoinNode.Exec()
  48. })
  49. bench.Exec("node Smart refl", func() interface{} {
  50. return srjoinNode.Exec()
  51. })
  52. bench.Exec("node native", func() interface{} {
  53. return strings.Join(strings.Fields(s), ",")
  54. })
  55. }