$function_name() vs call_user_func($function_name)
http://dikini.net/php_design_patterns
で、「$callable(...)」と「call_user_func($callable,...)」「call_user_func_array($callable,...)」について言明していた。
あいかわらず英語の内容を翻訳する気はゼロだけど、ニュアンス的に「$callable(...)」はキモイから使うなって言ってるのか?
よくわからないけど、ちょっと実行速度だけ適当に測定してみた。
define('REPLAY_NUMBER', 100000); $function_name = 'test_function'; $time_a[0] = microtime_ex(); for ($i = 0; $i < REPLAY_NUMBER; ++$i) { $function_name(); } $time_a[1] = microtime_ex(); $time_b[0] = microtime_ex(); for ($j = 0; $j < REPLAY_NUMBER; ++$j) { call_user_func($function_name); } $time_b[1] = microtime_ex(); echo "<pre>\n", "time_a is ", ($time_a[1] - $time_a[0]), "\n", "time_b is ", ($time_b[1] - $time_b[0]), "\n", "</pre>\n" ; function test_function() { sleep(0.001); } function microtime_ex() { $mtime = explode(' ', microtime()); $msec = (float) $mtime[0]; $sec = (float) $mtime[1]; return ($sec + $msec); }
[1回目]
time_a is 0.32629895210266
time_b is 0.5169689655304
[2回目]
time_a is 0.38656997680664
time_b is 0.59860301017761
[3回目]
time_a is 5.143630027771
time_b is 0.83590197563171
[4回目]
time_a is 0.33151888847351
time_b is 0.5154390335083
[5回目]
time_a is 0.43265199661255
time_b is 0.57433485984802
「$callable(...)」の方が速度だけでいえば早いのか?