doujin8476 2014-04-29 08:14
浏览 39
已采纳

我是否需要避免附加性能?

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条回答 默认 最新

  • drgawfsf1069 2014-04-29 08:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式