duanli9569 2015-06-04 19:25 采纳率: 100%
浏览 34
已采纳

有可能告诉php解释器忽略代码块吗?

Background

Back in the day when I did C programing we used assume statements to make sure the code was healthy. Something like:

aFunction(int hello)
{
  assume(hello < 10);

The cool thing about the assume statement is it was defined to be an empty macro for release compiling and a die on false statement on debug compiling. So the developer had all the benefits of an assert statement checking their assumptions without causing unnecessary bugs or affecting performance in

Problem

Now that I'm working php I'm looking for a similar mechanism. I currently have my assume statement looking something like this:

define(DEBUG, getenv('debug'));
function assume($a)
{
  if (DEBUG && !$a)
  {
    echo 'ASSUME FAILURE <br />';
    echo '<pre>';
    print_r(debug_backtrace());
    die('<pre>');
  }
}
assume($a == $b);

As you can see I set the debug flag in the server settings to remove the actual die statement and such. However a function call is still performed and an if statement is still evaluated. This is not a big deal if it only occurs a few thousand times but as the code get's bigger and as these assumes get put into loops this could have a legitimate affect on performance.

Question

Is there a way to tell the interpreter or opcache (I'm currently using zend opcache included in php 5.5) to ignore these assume statements altogether? or is there another method of handling this problem?

  • 写回答

1条回答 默认 最新

  • dongqian1893 2015-06-04 19:35
    关注

    The difference is that the C compiler is optimized and can eliminate blocks of code that are not used. In PHP, assume is a function call, and the function has to exist.

    A couple of solutions. First, you may use assert(). The assert function works similar to your own assume function. If an assertion fails, a defined assertion handler is called. You can easily enable or disable assertions, and if you disable them, the assertion handler is not called at all.

    If you want to proceed with your own assume function, you can still optimize it a little, by declaring the function based on the define:

    <?php
    define(DEBUG, getenv('debug'));
    
    if (DEBUG) {
      function assume($a) {
        echo 'ASSUME FAILURE <br />';
        echo '<pre>';
        echo print_r(debug_backtrace());
        die('<pre>');
      }
    } else {
      function assume($x) {}
    }
    

    This way, you actually declare assume as an empty function. The comment by Michael Berkowski confirms my fear that PHP doesn't eliminate the call completely, but at least you save the if.

    Another solution is to call assume a little more complex:

    DEBUG && assume($x < 10);
    

    This way, if DEBUG is false, the call is not made at all. This way only the boolean value is evaluated, and no function call is made. Of course, calling it this way is a bit harder and less readable, but I think the savings of not calling a function are much more significant than eliminating an if.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测