PHP5 - PHP5中的RTTI(獲取類名稱等)的成本是多少?

[英]PHP5 - What is the cost of RTTI (get class' name and etc.) in PHP5?

I notice that some PHP frameworks, such as Kohana and CodeIgniter would look at the class name of a class to perform auto-loading. Traditionally, RTTI is expensive for C++; how expensive it is for PHP5, in comparison to:

我注意到一些PHP框架,比如Kohana和CodeIgniter,會查看類的類名來執行自動加載。傳統上,RTTI對C ++來說很昂貴;與以下相比,PHP5的價格有多貴:

  1. A direct message call
  2. 直接留言

  3. Looking up a key in an associative array
  4. 查找關聯數組中的鍵

  5. Doing a message call via variables ( $class = 'foobar'; $method = 'foo'; $class->$method() )
  6. 通過變量進行消息調用($ class ='foobar'; $ method ='foo'; $ class - > $ method())

2 个解决方案



In general, if you are using PHP, performance should not be your biggest worry, write good looking code first (ie. readable and maintainable, self documenting etc.) then profile and optimize as needed. If you begin by worrying about speed, PHP is probably not the way to go.


But to answer your question... get_class has pretty decent performance, i think it's pretty well optimized inside the zend engine. trying to call a non-existing function and dealing with the error is much more expensive. (it is a fatal error to call a non existent function, you do not get to catch it unless you write a bunch of glue code in your base object)

但是要回答你的問題...... get_class具有相當不錯的性能,我認為它在zend引擎中得到了很好的優化。試圖調用一個不存在的函數並處理錯誤要貴得多。 (調用不存在的函數是一個致命的錯誤,除非你在基礎對象中寫了一堆粘合代碼,否則你不會捕獲它)

Here's a bit of benchmarking to show some of the different methods of determining the ability to run a method.




class MyClass {
    public function Hello() {
        return 'Hello, World!';

function test_get_class( $instance ) {
    $t = get_class( $instance );

function test_is_callable( $instance ) {
    $t = is_callable( $instance, 'Hello' );

function test_method_exists( $instance ) {
    $t = method_exists( $instance, 'Hello' );

function test_just_call( $instance ) {
    $result = $instance->Hello();

function benchmark($iterations, $function, $args=null) {
    $start = microtime(true);
    for( $i = 0; $i < $iterations; $i ++ ) {
        call_user_func_Array( $function, $args );
    return microtime(true)-$start;

$instance = new MyClass();

printf( "get_class:     %s\n", number_format(benchmark( 100000, 'test_get_class', array( $instance ) ), 5) );
printf( "is_callable:   %s\n", number_format(benchmark( 100000, 'test_is_callable', array( $instance ) ), 5) );
printf( "method_exists: %s\n", number_format(benchmark( 100000, 'test_method_exists', array( $instance ) ), 5) );
printf( "just_call:     %s\n", number_format(benchmark( 100000, 'test_just_call', array( $instance ) ), 5) );



get_class:     0.78946
is_callable:   0.87505
method_exists: 0.83352
just_call:     0.85176



The reason RTTI is expensive in C++ is because you need to have the extra step of look-up (vtable for example). Take function call as an example. It is as a result much slower than moving the program counter register to a memory address that hosts the function code.

RTTI在C ++中很昂貴的原因是因為你需要額外的查找步驟(例如vtable)。以函數調用為例。結果比將程序計數器寄存器移動到承載功能代碼的存儲器地址慢得多。

I haven't seen the implementation of PHP engine, but I suspect as an interpreting language, this "overhead" is either a must (given it doesn't compile the program, it needs to figure out where to call the function anyway), or is minimal compared with all the other overheads of an interpreting language.


That said, the best way to investigate is to do a simple experiment and profile the result. You can have two programs, one use RTTI to make function calls, and the other use direct calls, and compare the results from the two.




粤ICP备14056181号  © 2014-2020