Do I always have to think about performance?

后端 未结 21 720
小蘑菇
小蘑菇 2021-02-04 04:16

I come from a DBA world and performance has always been an obsession. I am turning to development and I always think about performance, constantly, all the time.

Reading

21条回答
  •  逝去的感伤
    2021-02-04 04:38

    Don't think about performance until after you've got it working correctly. If it works correctly, and it doesn't have any user noticable performance problems, don't optimize.

    If it works correctly and it has significant and noticable delays, don't optimize. Profile instead. Most of an application's time is going to be spent in a single "hot" loop, and which loop it is is seldom intuitive. You need real measurements and science to tell you what's happening. Once you have your profile data, your optimization task should progress from big to small:

    1. Architecture optimizations. Is the overall structure of the application the source of the inneficiency?

    2. Algorithm optimizations: Are you using the right data structures? Are you accessing them in the right way? Is your application spend most of its time writing, or most of its time reading? Optimize for the answer to that question.

    3. Last resort. Microoptimization. Streamlining the hot loops, or unrolling some loops. Duff's Device. Don't optimize at this level until you've determined that you can make no further improvements to the other two levels, and you still haven't met your performance goals. This level of optimization has a high likelyhood of breaking shit, and making your application more difficult to grow, more brittle, so don't do it unless you really really have to.

    Again I will emphasize, don't waste your time on optimizing just any random bit of code that looks inefficient. Optimization time is a significant investment. You should have evidence to back you up before you gamble your time on a loser.

提交回复
热议问题