0

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?

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
Core_dumped
  • 1,571
  • 4
  • 16
  • 34
  • 1
    Yes, please use a profiler for the big picture. Then look at the SQL queries instead. – mario Dec 26 '14 at 22:49
  • 2
    [PHP freshmen wants to optimize...](http://www.quickmeme.com/img/d2/d26f0ee993134aaee20c8e67eff93aa6d089cd82f912a68316a38de4a3b7c551.jpg) – PeeHaa Dec 26 '14 at 22:57
  • 1
    You use the __appropriate__ constructs for __readability__, not necessarily the fastest when the differences are measured in nanoseconds – Mark Baker Dec 26 '14 at 22:58
  • 1
    And often performance will vary depending on what you need to do inside your loops: e.g. if you need access to the associative key for an array, then there's little sense in using `for()` – Mark Baker Dec 26 '14 at 22:59
  • mark they are in percentages not nanoseconds. But the core of my question isn't this, but do these loops and conditions really affect the server's response time, or they play little role in this? – Core_dumped Dec 26 '14 at 22:59
  • And with the most recent versions of PHP, you also have an opcode optimizer that will change the underlying code structures to a degree anyway – Mark Baker Dec 26 '14 at 23:00
  • 1
    If you want something to run fast, the first thing you should optimise away is PHP itself. – Erik Dec 26 '14 at 23:22
  • 1
    @Erik So I guess that database queries or file access of remote calls are never a performance overhead in other languages? – Mark Baker Dec 26 '14 at 23:28
  • @MarkBaker: compared to *the specific loop or test/eval construct* used (inside of which lives the slow operation)? – frasnian Dec 26 '14 at 23:41

3 Answers3

2

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.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • Your using answers mostly based on intuitions, while I tested them myself. Anyhow, my question isn't about which is more performant, but is about performance in terms of these operations. Do these operation really affect the server response, or they do little differences – Core_dumped Dec 26 '14 at 22:54
  • Is your system perfect in other sections, such as algorithms complexity etc? I think that the differences are very small, unless you are using a system that desperately needs optimization in that level..... – Nick Louloudakis Dec 26 '14 at 23:00
0
  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.
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • But suppose a loop is executed 10 million times, and Suppose if it is a for loop it takes 8 secs, but if a while it takes 9 secs in total. Then suppose further at the same time a condition is executed 10 million times, and an if else takes 10 secs, while a switch takes 11 secs. And further a foreach which takes also 8 secs, while a for 8.5 secs. So it turns out that after 10 million runs, there is a 2.5 seconds difference. Is this not big? You may say 10 millions is a big number, but do we not in programming scale these operation in terms of big numbers? Millions, billions etc. – Core_dumped Dec 26 '14 at 23:21
  • Whatever is inside that loop will count for MUCH more than the looping construct itself. And if you're running 10 million calculations in a loop, you'd probably be better off running those calculations in a different language altogether. – Erik Dec 26 '14 at 23:24
  • Practically if you see the number 10 million, it can mean 2 things. 1. This amount of data is coming from somewhere. And accessing this somewhere will be the bottleneck (IO vs CPU). 2. Something is already scaled wrongly, and it is much more efficient to try to fix the scaling issue, which can convert the 10 million, to 10 thousand, than wasting time for the few percent. – Lajos Veres Dec 26 '14 at 23:26
  • I'm not saying 10 millions for a singe execution, suppose thousands of times there have been requests and in total this number of iterations was executed in say 1 hour – Core_dumped Dec 26 '14 at 23:28
  • I am sure that we can find/create scenarios where the few percent matters, but in the reality this is very rare. At least based on my experience. – Lajos Veres Dec 26 '14 at 23:30
0

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.

frasnian
  • 1,973
  • 1
  • 14
  • 24
  • 1
    If want the code of my "grandmother's life-support system" to be readable and maintainable thank you very much. Unless you made a typo and you meant "mother in law's" – PeeHaa Dec 26 '14 at 23:08
  • As do I, @PeeHaa. Hence trotting out the old "make it readable" saw. I have had to write code where software failures would have real-world consequences (beyond money), and in those cases the focus was *always* on correctness, testability, and readability - and those things usually go hand-in-hand (-in-hand? how many hands?). – frasnian Dec 26 '14 at 23:13
  • @PeeHaa: *snerk* Just read the edit to your comment. You owe me Windex for my monitor. Also, I was sorry to hear that you would not be designing the network or coming up with an IP address allocation solution for "SeeM3FictionalNetwork" or whatever it was. I would have probably PeeHaa'd on myself to read it. – frasnian Dec 26 '14 at 23:17