1. What is opcode
After the interpreter parses the code, generates an intermediate code that can be run directly, called the opcode, opcode
2. The difference between interpreter and compiler
The interpreter runs the intermediate code directly after the intermediate code is generated, and the control of the runtime is still in the interpreter's hand.
The compiler then further optimizes the code after generating the intermediate code, generating a target program that can run directly, but not executing, waiting for the user to trigger execution, his control over the target program, and compiler-independent.
3.php is an interpretive language, and his principles are a little similar to compilation, including lexical analysis, grammatical analysis, semantic analysis ... the core engine of the PHP interpreter is Zend.
4.php How to view the opcode of a piece of code
Install PHP parsekit extension, through the extended API can see PHP opcode, such as parsekit_compile_string ()
Php-r "Var_dump (parsekit_compile_string (' Print +; '));"
5.opcode has a Build
First, through lexical analysis, the script code can be seen as a series of word combinations, the interpreter to classify these words, and marked
For example, print, we look at the PHP source package zend/zend_language_scanner.l In this file we can find the pirnt corresponding to the tag. T_print
After the tag is found, then there is the parsing, and in Zend/zend_language_parser.y we can find the corresponding function of T_print.
Then find this function implementation code in ZEND/ZEND_COMPILE.C, this function is to implement the opcode transformation. All opcode are represented by a user integer.
6. Turn on opcode cache
Generating opcode is a system overhead, and every execution is generated once opcode, so the overhead is considerable, so PHP's optimizations must turn on the opcode cache to avoid duplication of compilation.
PHP's opcode cache has apc,eaccelerator,xcache, all of which put opcode in shared memory.
Take APC For example: Set Apc.cache_by_default = on in php.ini
by <?php Print_r (Apc_cache_info ());? > can view the cache situation,
7.opcode Cache Expiration
OpCode cache will expire, if the expiration is to be reborn once, of course, can also skip the mechanism of expiration check, set in PHP.ini Apc.stat=off
This changes the program code to take effect by restarting the server.
8. Script tracking and analysis, can use Xdebug to track, with Xdebug can implement performance tracker, find the program execution bottleneck, thus optimizing the program.
Xdebug.profiler_output_dir =/tmp/xdebug
Xdebug.profiler_output_name = cachegrind.out.%p
function tracking for Xdebug:
Xdebug.trace_output_dir =/tmp/xdebug
Xdebug.trace_output_name = trace.%c
Under Window you can view the Xdebug report file with Wincachegrind.
PHP opcode Cache