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 个解决方案

#1


3  

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.

一般來說,如果你使用PHP,性能不應該是你最大的擔心,首先編寫好看的代碼(即可讀和可維護,自我記錄等),然后根據需要進行分析和優化。如果你從擔心速度開始,PHP可能不是你要走的路。

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.

這里有一些基准測試,以顯示確定運行方法的能力的一些不同方法。

benchmark.php:

<?php

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) );

?>

results:

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

#2


0  

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.

我沒有看到PHP引擎的實現,但我懷疑作為一種解釋語言,這個“開銷”要么是必須的(假設它不編譯程序,它需要找出在哪里調用函數),或者與解釋語言的所有其他開銷相比是最小的。

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.

也就是說,調查的最佳方法是進行簡單的實驗並對結果進行分析。您可以有兩個程序,一個使用RTTI進行函數調用,另一個使用直接調用,並比較兩者的結果。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/11/08/7299d395bfeffd97371c9392c2edcd4a.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com