3. Use AB to test the speed of two php scripts: (ps: an astonishing time has begun)
Bad. php
Goods. php
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141205/20141205084010231.png" alt = "">
For these two tests, we can see that the difference is not half past one. When I run bad. php, I obviously feel the computer fan, you know!
Bad. php responds to 174 requests per second, with each request processing 572 ms
Goods. php responds to 4050 requests per second. Each request is processed for 24 ms.
Ps: If you want your friends to use the bad statement, get rid of it and be careful not to know it!
4. Reasons
I was surprised by this result, but the fact is just in front of me. No way, why is this effect? Let's continue to look at it:
*. Php (php code) ----- parse (the zend engine performs a row-by-row scan to change to a syntax that zend can recognize) ----> exprs
----- Parser (resolved to opcode) -----> opcodes ------ exec (executed final output) -------> output
We can know that zend needs to be scanned for a php file and parsed to opcode for output execution. The problem lies here. When we use php's own functions, it actually belongs to the zend engine, therefore, zend is easy to parse, and the speed is fast (goods. php), but when the execution is bad. php is slow, because reading the code written by others is certainly not reading the code written by yourself. You are right, so it is better to use originality instead of making your own wheels!
To put it another way, this is why some Php extensions (such as apc) cache opcode. Because of this, scanning and parsing are missing, it must be faster!
5. Summary
Optimization: less code writing and more php capabilities
Performance problems:
The code written by myself is redundant, hard to read, and of low performance.
Why is performance low?
Php code needs to be compiled and parsed into the underlying language. This process will be processed every time, with high overhead.
Good method:
Use php built-in variables, constants, and functions (spl can provide you with easy-to-use functions)