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条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接