从PHP5.1开始,PHP提供了用户对Zend VM执行分发方式的选择接口.
之前的文章中, 我也提过这方面的内容, Zend虚拟机在执行的时候, 对于编译生成的op_array中的每一条opline的opcode都会分发到相应的处理器(zend_vm_def.h定义)执行, 而按照分发的方式不同, 分发过程可以分为CALL, SWITCH, 和GOTO三种类型.
默认是CALL方式, 也就是所有的opcode处理器都定义为函数, 然后虚拟机调用. 这种方式是传统的方式, 也一般被认为是最稳定的方式.
SWITCH方式和GOTO方式则和其命名的意义相同, 分别通过switch和goto来分发.
官方给出的描述是:
CALL - Uses function handlers for opcodes
SWITCH - Uses switch() statement for opcode dispatch
GOTO - Uses goto for opcode dispatch (threaded opcodes architecture)
GOTO is usually (depends on CPU and compiler) faster than SWITCH, which
tends to be slightly faster than CALL.
CALL is default because it doesn’t take very long to compile as opposed
to the other two and in general the speed is quite close to the others.
那么如果使用GOTO方式, 效率上到底能提高多少呢?
今天我就分别使用各种方式来测试一番, 测试脚本bench.php.
第一点被证明的就是, 官方说的GOTO方式编译耗时显著高于其他俩种方式, 我一开始在虚拟机上编译, 每次都Hangup(