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 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?