在性能方面,我是否需要避免使用追加操作?

huangapple go评论73阅读模式
英文:

Do I have to avoid appending for performance?

问题

我是新手学习 Golang。
我应该总是避免使用 append 来添加切片吗?

我需要将一个以换行符分隔的数据文件加载到内存中。
考虑到性能,我应该先计算行数,然后将所有数据加载到预定义长度的数组中,还是可以直接将行添加到切片中?

英文:

I am new to Golang.
Should I always avoid appending slices?

I need to load a linebreak-separated data file in memory.
With performance in mind, should I count lines, then load all the data in a predefined length array, or can I just append lines to a slice?

答案1

得分: 11

你应该停止考虑性能问题,开始测量你的应用程序的实际瓶颈是什么。

对于像“因为性能问题,应该做/避免X吗?”这样的问题,50%的情况下给出的建议是无用的,25%的情况下是适得其反的。

有一些非常通用的建议,比如“不要不必要地生成垃圾”,但是你的问题无法回答,因为这在很大程度上取决于你的文件大小:

  • 你的文件大小约为3TB?很可能你无论如何都需要逐行读取它...
  • 你的文件只有一堆(约50行):可能首先计算行数比重新分配[]string切片4次(或者初始时使用make([]string,0,100))更费力。一个string只占用2个字。
  • 你的文件有一个未知但很大(>10k)的行数:也许值得尝试。“也许”是指你应该在真实数据上进行测量。
  • 你的文件已知很大(>500k行):肯定首先计算行数,但你可能会从第一个问题点开始遇到问题。

你看:关于性能的通用建议是一个糟糕的建议,所以我不会给出一个。

英文:

You should stop thinking about performance and start measuring what the actual bottleneck of you application is.

Any advice to a question like "Should do/avoid X because of performance?" is useless in 50% of the cases and counterproductive in 25%.

There are a few really general advices like "do not needlessly generate garbage" but your question cannot be answered as this depends a lot on the size of your file:

  • Your file is ~ 3 Tera byte? Most probably you will have to read it line by line anyway...
  • Your file has just a bunch (~50) of lines: Probably counting lines first is more work than reallocating a []string slice 4 times (or 0 times you you make([]string,0,100) it initially). A string is just 2 words.
  • Your file has an unknown but large (>10k) lines: Maybe it might be worth. "Maybe" in the sense you should measure on real data.
  • Your file is known to be big (>500k lines): Definitively count first, but you might start hitting the problem from the first bullet point.

You see: A general advice for performance is a bad advice so I won't give one.

huangapple
  • 本文由 发表于 2014年4月29日 16:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/23359156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定