dongxidui1227 2014-01-04 00:56
浏览 48
已采纳

Golang效率中的非通用映射

I completely understand that Go doesn't provide support for Generics, opting instead for users to create their own type-specific methods when needed.

However, I am wondering if there is a more efficient way to make a specific map function over a data structure that does not involve looping through the entire list and applying the function, or is this what other languages with generics support do behind the scenes.

Example:

func map(list []string, op func(string)string) []string {
  ouput := make([]string, len(list))
  for i, v := range list {
    output[i] = op(v)
  }
  return output
}

Thanks!

  • 写回答

2条回答 默认 最新

  • dongsilu2237 2014-01-04 01:14
    关注

    FYI, map is a reserved word, so you can't make your function exactly as written with lowercase map.

    That is probably as good as you can get. Generics don't get you around allocating new memory.

    It is slightly faster to use append() rather than initializing the whole slice to empty strings at the beginning. For example:

    func Map(list []string, op func(string) string) []string {
       output := make([]string, 0, len(list))
       for _, v := range list {
          output = append(output, op(v))
       }
       return output
    }
    

    This gave me about a 10% increase in speed.

    Update: That was only true for very short slices. Here's a more thorough benchmark--it was actually slower to use append on longer slices. I also tried parallelizing it, which was only worth the overhead on much bigger slices.

    Code: https://gist.github.com/8250514

    Output (numbers at end of test names are slice lengths):

    go test -bench=".*" -test.cpu=2
    BenchmarkSliceMake10-2       5000000           473 ns/op
    BenchmarkSliceMake100-2       500000          3637 ns/op
    BenchmarkSliceMake1000-2       50000         43920 ns/op
    BenchmarkSliceMake10000-2       5000        539743 ns/op
    BenchmarkSliceAppend10-2     5000000           464 ns/op
    BenchmarkSliceAppend100-2     500000          4303 ns/op
    BenchmarkSliceAppend1000-2     50000         51172 ns/op
    BenchmarkSliceAppend10000-2     5000        595650 ns/op
    BenchmarkSlicePar10-2         500000          3784 ns/op
    BenchmarkSlicePar100-2        200000          7940 ns/op
    BenchmarkSlicePar1000-2        50000         50118 ns/op
    BenchmarkSlicePar10000-2        5000        465540 ns/op
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?