2

一个 8byte 的内存优化

 3 years ago
source link: https://www.v2ex.com/t/799442
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go

一个 8byte 的内存优化

  newmiao · 44 分钟前 · 183 次点击

最近看 Dave Cheney 的一篇文章,发现一个有趣的代码片段,里面提到了一个 8byte 的内存优化。

代码片段是这样的:

func BenchmarkSortStrings(b *testing.B) {
 s := []string{"heart", "lungs", "brain", "kidneys", "pancreas"}
 b.ReportAllocs()
 for i := 0; i < b.N; i++ {
  var ss sort.StringSlice = s
  var si sort.Interface = ss
  sort.Sort(si)
 }
}

代码很简单,是对一个[]string做排序的内存分配压测

详见 传送门


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK