-2

I'm curious what the impact on the server is when PHP if statements are evaluated, i.e. memory consumption and CPU usage and if this could become a major issue as traffic grows?

For example, if I use a lot of PHP IF statements in the theme for each post summary on a WordPress blog, is this going to require a great deal more server resources if the home page starts getting high traffic? And, on pages where many posts are listed (i.e. category pages) , it seems like this could become a problem.

I'm thinking it may be good idea to pare down the IF statements that aren't absolutely necessary in the event that traffic spikes. Or, is this even something I be worrying about?

undoIT
  • 611
  • 1
  • 7
  • 13

4 Answers4

10

The time spent evaluating 1000s of if statement will be completely overwhelmed by a single database query.

You need to:

  1. Profile to find out where your bottlenecks are
  2. Focus - if a function takes 50% of the total time then making it 10% faster will have more effect than making a teeny function 90% faster.

Edit:

Database queries are slow (even a quick one) - you should aim to make as few queries as possible, within reason. Within a query, I don't think there's a speed difference between checking for NULL or a specific value - if there is it will be a minuscule difference.

Greg
  • 316,276
  • 54
  • 369
  • 333
  • Ok, thanks. Most of the IF statements I'm using check for values in the database i.e. IF NOT NULL or if a specific value exists. So then, the IF statement itself may not be an issue, but the fact that it is creating a database query is. Is there any difference between checking if a field is not null vs. checking if the field has a specific value related to the current post? – undoIT Aug 18 '09 at 16:20
  • +1 for adding "within reason" as a caveat to your DB queries warning. Do not try to condense queries to the point that they are so ridiculously complex that they just cause more overhead anyway. – Josh Leitzel Aug 18 '09 at 16:41
2

I think this is something you don't have to worry about : if your application does one or two queries to the database, those will take much more time than a couple (even more than a couple) of "if".

This is kind of "micro-optimization" : if you are looking for big optimizations in the event of a traffic spike, there are other ways that should be explored first ;-)

For some (quite longer) explanations / ideas, you can take a look at this answer I gave yesterday to another question.
(I really don't feel like rewriting any of that before at least a couple of days have passed ^^ )

Basically, what can really help is :

  • Using an opcode cache, like APC
  • Using caching : the more data you cache, the less calculations / DB queries you have to make -- and those are the one that generally take the most important amount of time
  • Using Xdebug and it's profiling abilities, to identify what takes time in your application
  • Optimizing SQL queries, using the right indexes
  • If you want to go farther, you can use a reverse-proxy to act as another level of cache ; for instance, varnish.
Community
  • 1
  • 1
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • @downvoter : I'm fine with being downvoted : it's part of the way SO works ; but I would **really** like to know what is wrong in what I say : this is the only way I won't do that mistake on the next website I'll give optimization advices for... If what I said is plain wrong, tell me : I need to know so I don't say the same sh*t again the next time ! (and, btw, I have the same feeling when other people get downvoted : not explaining what was wrong is sometimes plain rude, and doesn't help anyone understanding what could be done better :-( ) – Pascal MARTIN Aug 16 '09 at 21:08
  • Hi Pascal. Thank you for the answer. How about eAccelerator? That is what I currently have installed. I've seen a couple people recommend APC so I'm wondering if it is better. – undoIT Aug 18 '09 at 16:25
  • Hi, I've never used eAccelerator, and always heard about APC in great terms, so I can't really say ; sorry... – Pascal MARTIN Aug 20 '09 at 19:04
2

Nothing to worry about, at all.

Havenard
  • 27,022
  • 5
  • 36
  • 62
0

I'm thinking we need to give a more logical reason why it doesnt matter.... it's because an IF statement at it's basic level in assembler comes down the the JNZ mnemonic. It's a 1 "word" value meaning "JUMP IF NOT ZERO". The real heavy load will come from the comparing and evaluating the conditionals, not the IF statement itself.

Joshua K
  • 457
  • 3
  • 12