Performance: if else vs switch, while vs for, for each vs for, print vs printf [closed]

白昼怎懂夜的黑 提交于 2019-12-24 12:36:13

问题


I was testing all these different constructs using getrusage() to calculate the variations of (ru_utime + ru_stime) before and after executing them.
It turns out there's no little difference in performing the same task for similar constructs. Here are the results:
printf (1.5 ± 0.5)% faster than print
foreach (6.0 ± 1.0)% faster than a for loop(iterating over an indexed array of 1kk elements)
for (9.0 ± 1.0)% faster than a while loop
if/else (8.0 ± 1.0)% faster than a switch(tested over two cases, 6 cases, and 10 possible cases).

So I was wondering, do these differences really matter? If we use all the most efficient such constructs in our code, will it make some difference? Maybe 6%, 8% there, 9% or 10% there, summed up will change the efficiency of our code?
Or will these differences nonetheless be not noticeable, I mean the response of the server to requests will almost not vary? Also, if we use if/else over switch, for over while, printf over print, foreach over for, will it eat up more RAM?


回答1:


Answering your question using StackOverflow answers, in terms of performance:

For if/else vs switch: Same performance (more).

For for vs foreach vs while: It doesn't matter (more).

For printf vs print: One not better than the other (more).

My humble opinion is that you use whatever seems more comfortable to you and/or covers your needs better (there are slight differences between each other, in other terms than performance, and this depends on what you need). In terms of performance, there are other things that you should take care of in order to maximize performance, such as decreasing the O complexity of code used in your program, network delays handling, parallelization of tasks when needed etc. Your question is a little bit general, so I tried to answer the same way.




回答2:


  1. You are right. Few percent doesn't really matter.
  2. The scalability of the operations can much more dramatically influence your application's performance than these few percent. Scalability means in this sense how your system will behave if you receive 2x, 10x more amount of data. Will your system be slowed down only 2x, 10x (if it is linear), or 4x, 100x (quadratic) etc. http://en.wikipedia.org/wiki/Scalability
  3. This problem usually occurs on database side as other mentioned. There are the "big" operations. There are the data which can be big.
  4. Most of the developers don't have to care too much about resources. And yes. profiling is the proper way to handle performance issues.



回答3:


OP writes:

Do these differences really matter? If we use all the most efficient such constructs in our code, will it make some difference?

Well, yes and no. Are you programming ultra-low latency applications? Hard (deterministic) real-time applications? If yes, then yes. Waiting on keyboard input from a user? It's a miracle your CPU doesn't die from boredom. So, no.

A 9% difference may be negligible (your Elf-Lord doesn't kill the troll), or it might extremely significant (your grandmother's life-support system kills her).

Basically, this is not a question that can be answered here. Setting aside environment/execution/compiler optimization issues: it's not only a book-length subject, but the specific book would depend on what kind of programs are you talking about, and what the consequences and requirements are for the performance envelope. I would guess that for 99.99% of programmers, the difference between a switch or if/else statement, or similar peephole optimization, is (in real terms) absolutely zero.

The old maxim "make it readable, make it run, then make it run fast if you have to" is probably your best course of action. Even in that extremely rare case where it might matter, just be sure to use a profiler to determine where the "where" part of the "make it run fast" phase is really needed.



来源:https://stackoverflow.com/questions/27662753/performance-if-else-vs-switch-while-vs-for-for-each-vs-for-print-vs-printf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!