hi i am building a large application using codeigniter
. because the application is very large , performance is a must . i want to optimize my if else statements .
is there is difference of performance using from
if(false) and if(0)
if(true) and if(1)
what is the efficient way ?
and when using logical operators is their any sequence that optimize the 开发者_Go百科performance
from if((a & b) || c)
and if(c || (a & b) )
what is the efficient way ? or those two are the same ?
and from
if(a && b)
and if(a & b)
what is the efficient way ?
and from
if(a || b) and if(a | b)
what is the efficient way ?
is there any performance difference between if(a || b) and if((a || b))
note that 2 nd if is in two additional parentheses
please help me ................................... thanks in advance.
I would't mess with the 0
and false
/1
and true
distinction unless you know the piece of code using it is a bottleneck; that's really micro-optimization.
As for |
vs ||
and &
vs &&
, use the double-character ones; they short-circuit. As for the order of the parts, the first one will always be evaluated (if the &&
or ||
is evaluated, that is), but the second one will only be evaluated if the first one doesn't yield the final result. Thus, the first part should be quicker than the second. If they're both equally fast, it shouldn't matter which order they're in.
Extra parentheses might slow down the parser a minute amount but you can cache bytecode, so that's not really much of an issue.
This is also known as "micro-optimization", and generally frowned upon. The difference is hardly measurable at all. If you can shave off 50% of your if "calculation" time (which is a lot for optimization) you will end up with exactly no difference in the end product, unless your code is only if's and nothing else. Making 1 query, file operation or even a loop/foreach a tiny bit quicker would make more difference. Write your ifs for readability, not for "speed".
Last but not least: for every performance-based change you need to MEASURE. Do isolated and repeated tests to find out what is quicker.
None of these optimizations will help you much. PHP code will never be a bottleneck unless you are running some serious data processing application like calculating pi to a million digits. No matter how large your application is what you need to make sure is:
1)Minimum file operations
2)Most of your queries should use an index
My reply may be irrelevant but that would only be true in .01% of cases
Php spends most of its execution time waiting for sql response, so don't focus on these small optimizations, they won't make a difference. Use microtime() to monitor each step's execution time if you still doubt.
精彩评论