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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等