PHP kullanıyorsanız, genel olarak, performans (yani okunabilir ve sürdürülebilir, kendini belgeleyen vb) ilk iyi görünümlü kod yazmak, sizin en büyük endişe olmamalı sonra profili ve gerektiği gibi optimize. Eğer hız endişesi ile başlarsanız, PHP muhtemelen gitmek için yol değildir.
Ama sorunuza cevap ... get_class oldukça iyi bir performansa sahiptir, i oldukça iyi zend motorun içinde optimize düşünüyorum. Mevcut olmayan bir işlevi çağırmak için çalışıyor ve hata ile ilgili much daha pahalıdır. (Bir olmayan işlevi çağırmak için bir ölümcül hatadır, size temel nesnenin içinde tutkal kod bir demet yazmak sürece onu yakalamak için alamadım)
İşte yöntemini çalıştırmak için yetenek belirleme farklı yöntemlerden bazılarını göstermek için kıyaslama bir parçasıdır.
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) );
?>
Sonuçlar:
get_class: 0.78946
is_callable: 0.87505
method_exists: 0.83352
just_call: 0.85176