関数に別名を付ける

WindowsLinuxでまったく異なる結果になった。

  • Windowsの場合
    • [ 2.0211 sec ] [hqq]
    • [ 1.7075 sec ] [hqq2]
    • [ 1.5741 sec ] [htmlspecialchars]
  • Linuxの場合
    • [ 0.4577 sec ] [hqq]
    • [ 0.2852 sec ] [hqq2]
    • [ 0.8502 sec ] [htmlspecialchars]
<?php
define('NUM', 100000);
define('Q', '\'');
define('QQ', '"');

$time=microtime(true);
for($i=0;$i<NUM;$i++){  hqq('test'); }
$end_time=sprintf("%01.04f", microtime(true)-$time);
echo '[ '.$end_time.' sec ] [hqq]'.BR;

$time=microtime(true);
for($i=0;$i<NUM;$i++){  hqq2('test'); }
$end_time=sprintf("%01.04f", microtime(true)-$time);
echo '[ '.$end_time.' sec ] [hqq2]'.BR;

$time=microtime(true);
for($i=0;$i<NUM;$i++){  '"'.htmlspecialchars('test', ENT_QUOTES, CHARSET).'"'; }
$end_time=sprintf("%01.04f", microtime(true)-$time);
echo '[ '.$end_time.' sec ] [htmlspecialchars]'.BR;

 function q($strings){
   return Q.$strings.Q;
 }
 function qq($strings){
   return QQ.$strings.QQ;
 }
 function h($string){
   return htmlspecialchars($string, ENT_QUOTES, 'EUC-JP');
 }
 function hq($string){
   return q(h($string));
 }
 function hqq($string){
   return qq(h($string));
 }
 function hqq2($string){
   return '"'.htmlspecialchars($string, ENT_QUOTES, 'EUC-JP').'"';
 }

?>