拥有更多<! - ?php标签效率不高? - >

时间:2013-01-24 23:56:53

标签: php

我一直都是一个编写简洁易读的代码的人。话虽如此,当使用更多或更少的<?php标签时,我无法确定处理速度是否有任何差异。这是一些用于比较的代码:

首选代码:

<?php while($condition): ?>
    <?php if($anotherCondition): ?>
        Hello world!
    <?php endif; ?>
<?php endwhile; ?>

SHORTER CODE:

<?php while($condition):
    if($anotherCondition): ?>
        Hello world!
    <?php endif;
endwhile; ?>

您在第二个代码中看到我只使用了两个<?php标记,但我发现第一个代码块更容易理解。是否有任何效率损失,因为我使用了更多<?php标签?

5 个答案:

答案 0 :(得分:3)

没有语法问题会影响性能,因此请编写可读代码:)

你的第二个例子也是可读的,没有理由使用4而不是2个php块

答案 1 :(得分:3)

存在性能影响,但可能忽略不计。不在<?php ?>块内的任何空格或制表符将作为PHP脚本输出的一部分发送。要理解这一点,请考虑这个简单的例子:

PHP代码:

<?php while($condition): ?>
    <?php if($anotherCondition): ?>
        Hello world!
    <?php endif; ?>
<?php endwhile; ?>

的形式通过网络发送
\n
\t\n
\t\tHello world!\n
\t\n
\n

虽然这个PHP:

<?php while($condition):
    if($anotherCondition): ?>
        Hello world!
    <?php endif;
endwhile; ?>

返回:

\n
\t\tHello world!\n
\n

这不是你真正想要担心的事情,我说去寻找可读的代码。不过我会说你的第一个例子涉及很多额外的打字,你确定它更好吗?

答案 2 :(得分:2)

由于你缺少的缩进,也许它难以理解? 我就是这样做的。

<?php
  while($condition): //should be a colon not a semicolon
    if($anotherCondition): //should be a colon not a semicolon
?>
      Hello world!
<?php 
    endif;
  endwhile; 
?>

答案 3 :(得分:2)

我测试了您的2种代码+ 1 无空间版本,通过在代码之前引入$time_start = microtime(true);并在代码之后引入echo $time = microtime(true) - $time_start;来检查处理速度。

由于处理时间接近微秒,因此结果可能会因许多微小因素而异。所以我测试了每个代码10次,平均时间。

使用打印文本进行测试

首选代码

<?php $time_start = microtime(true); ?>

<?php $i = 0; ?>
<?php while($i <= 5000): ?>
<?php echo $i; ?>
<?php $i++; ?>
    <?php if($i == 5000): ?>
        This is the end! 
    <?php endif; ?>
<?php endwhile; ?>

<?php echo $time = microtime(true) - $time_start; ?>

平均时间:0.00366528034210207秒

更短的代码

<?php

    $time_start = microtime(true);

    $i = 0;
    while($i <= 5000):
        echo $i." ";
        $i++;
        if($i == 5000):
            echo "This is the end!";
        endif;
    endwhile;

    echo $time = microtime(true) - $time_start;

?>

平均时间:0.00243649482727052秒

无空间代码

<?php $time_start=microtime(true);$i=0;while($i<=5000):echo $i." ";$i++;if($i==5000):echo "This is the end!";endif;endwhile;echo$time=microtime(true)-$time_start;?>

平均时间:0.00242624282836913秒

没有打印文本的测试

首选代码

<?php $time_start = microtime(true); ?>

<?php $i = 0; ?>
<?php while($i <= 5000): ?>
    <?php $i++; ?>
    <?php if($i == 5000): ?>
        <?php $a=$i; ?>
    <?php endif; ?>
<?php endwhile; ?>

<?php echo $time = microtime(true) - $time_start; ?>

平均时间:0.00143785476684571秒

更短的代码

<?php

    $time_start = microtime(true);

    $i = 0;
    while($i <= 5000):
        $i++;
        if($i == 5000):
            $a=$i;
        endif;
    endwhile;

    echo $time = microtime(true) - $time_start;

?>

平均时间:0.000472831726074218秒

无空间代码

<?php $time_start=microtime(true);$i=0;while($i<=5000):;$i++;if($i==5000):$a=$i;endif;endwhile;echo$time=microtime(true)-$time_start;?>

平均时间:0.000457286834716797秒

结论/摘要

打印文字
首选代码:0.00366528034210207秒 更短的代码:0.00243649482727052秒(比以前快33.5%)
无空间代码:0.00242624282836913秒(比之前快0.4%)

不打印文字
首选代码:0.00143785476684571秒 更短的代码:0.000472831726074218秒(比之前快66.1%)
无空间代码:0.000457286834716797秒(比以前快3.3%)

平均10次并不是真的正确。它应该通过删除极值结果100或1000次来完成,以获得非常好的表示。但是通过这个简单的例子我们可以看到2个第一个代码之间的显着差异,第3个代码是无关紧要的。

答案 4 :(得分:1)

我喜欢用代码的单独行中的转义格式进行格式化,个人化。

<?
php while($condition);
    if($anotherCondition); 
?>
        Hello world!
<?
    php endif;
endwhile; 
?>

这样,在阅读代码时,东西有点消失。

您使用的块数并不重要。

相关问题