1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Do "Notice" and "warnings" slow a PHP script, with no error reporting?

Discussion in 'PHP' started by JEET, Nov 10, 2019.

  1. #1
    Do "Notice" and "warnings" slow a PHP script, with no error reporting?

    I once did a test for this, and the difference in execution time was negligible.
    I am still curious though.

    If error_reporting is set to zero, and "notice" errors are getting generated, does it matter much?
    SEMrush
    This code for example will generate NOTICE errors:
    
    <?php
    
    $a=array( 'red'=>255, 'green'=>200, 'blue'=>100 );
    echo $a[red].'<br />'; //Notice error
    echo $a['green'].'<br />'; //notice error
    echo $a["blue"].'<br />'; //no error
    ?>
    
    Code (markup):
     
    JEET, Nov 10, 2019 IP
    SEMrush
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,254
    Likes Received:
    27
    Best Answers:
    7
    Trophy Points:
    150
    #2
    I would say yes. Notices and Errors point to code that isn't quite working as expected. I always try to have no error_log present. If the error_log file shows up I always tend to fix the issues asap.
     
    stephan2307, Nov 11, 2019 IP
    sarahk and JEET like this.
  3. JEET

    JEET Notable Member

    Messages:
    2,606
    Likes Received:
    185
    Best Answers:
    4
    Trophy Points:
    215
    #3
    Let me run a test again, Will post findings. Last time I did, I did with a small script.
     
    JEET, Nov 11, 2019 IP
  4. JEET

    JEET Notable Member

    Messages:
    2,606
    Likes Received:
    185
    Best Answers:
    4
    Trophy Points:
    215
    #4
    Ran this test.

    Proper code, no errors generated.
    
    <?php
    //error_reporting(0);
    $s=time();
    $a=array( 'red'=>255, 'green'=>126, 'blue'=>50 );
    for($x=0; $x<5000; ++$x){
    echo $a["red"]."\n";
    }
    $e=time();
    echo '<h3>'. ($e-$s).'</h3>';
    ?>
    
    Code (markup):
    Time taken is "0" seconds. ($e-$s)

    Now with error_reporting to zero, and code which will generate notice error

    
    <?php
    error_reporting(0);
    $s=time();
    $a=array( 'red'=>255, 'green'=>126, 'blue'=>50 );
    for($x=0; $x<5000; ++$x){
    echo $a[red]."\n";
    }
    $e=time();
    echo '<h3>'. ($e-$s).'</h3>';
    ?>
    
    Code (markup):
    Time taken is again "0" seconds

    If I comment error_reporting line in second code, then each time $a[red] is accessed, a notice error was generated.
    Even then execution time was just "1" second...
    And with all those NOTICE lines in browser, 5000 times, both Firefox and chrome hanged. Only IE worked.

    But with error_reporting set to zero, it was "0" seconds in both cases, and no NOTICE errors thrown to browser.
     
    JEET, Nov 11, 2019 IP
  5. SpacePhoenix

    SpacePhoenix Well-Known Member

    Messages:
    129
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    120
    #5
    Just ran that in both Chrome and Firefox (with the error_reporting line commented out - have got error turned on by default), neither of them hung. Can't test in Edge as i've uninstalled that
     
    Last edited: Nov 11, 2019
    SpacePhoenix, Nov 11, 2019 IP
    JEET likes this.
  6. JEET

    JEET Notable Member

    Messages:
    2,606
    Likes Received:
    185
    Best Answers:
    4
    Trophy Points:
    215
    #6
    Well, I am getting 5000 lines of Notice errors thrown in browser and both browsers hung.

    Anyways, that is not the issue. The issue is whether PHP processing takes longer or not.
    There was no difference in both codes with error_reporting set to zero.
     
    JEET, Nov 11, 2019 IP
  7. GrumpyYoungMan

    GrumpyYoungMan Peon

    Messages:
    1
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #7
    Try the same using microtime(true) instead of time()

    
    <?php
    error_reporting(0);
    $s=microtime(true);
    $a=array( 'red'=>255, 'green'=>126, 'blue'=>50 );
    for($x=0; $x<5000; ++$x){
    echo $a[red]."\n";
    }
    $e=microtime(true);
    echo '<h3>'. ($e-$s).'</h3>';
    Code (markup):
     
    GrumpyYoungMan, Jan 4, 2020 IP
    JEET likes this.
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,105
    Likes Received:
    1,691
    Best Answers:
    239
    Trophy Points:
    515
    #8
    1) due to timer granularity, (how often it is updated) looping a specified number of loops is NOT a meaningful way of benchmarking in PHP. Wait for timer rollover, then loop a specific amount of time counting iterations. Good rule of thumb any time you see "time elapsed" being used in a benchmark and the time difference is measured at anything less than ten seconds, it's card stacked or inaccurate bullshit.

    2) Why would $a['green'] make an error in your initial post? Oh, it doesn't... look at that.

    3) Echo means caching, networking, memory buffering, and a whole host of other factors could be interfering, so perhaps do a simple addition instead?

    So let's make a MEANINGFUL measurement of your second example.

    
    <?php
    // error_reporting(0);
    $test = [ 'red' => 255 ];
    $count = 0;
    $temp = microtime(true);
    do { $end = microtime(true); } while ($end == $temp);
    $end += 1; // 1 full second minimum
    
    do {
    	$temp += $test['red'];
    	$count++;
    } while (microtime(true) < $end);
    
    echo '<br><br>Iterations (higher is better) :', $count, '<br>';
    
    
    Code (markup):
    On my i7 4770k it gives me over 10 passes: (remember, higher is better)

    Valid with reporting enabled: 15,698,688 to 15,702,565 iterations.
    Valid with reporting disabled: 15,078,511 to 15,124,391 iterations
    Invalid with reporting enabled: 13,320 to 13,338 iterations (duh, outputting errors)
    Invalid with reporting disabled: 1,786,715 to 1,892,118

    Paints a pretty clear picture, no? Pretty much valid code with reporting enabled or disabled is eight or nine times faster than invalid with reporting disabled, over 1000 times faster with reporting enabled... and that's without subtracting the overhead of the loop and time code checking from the equation.

    Of course the reason it's 1000 times faster is that output is involved... which is why I removed the output overhead from the testing where possible.

    Also don't try to run all the checks in one file, caching, result buffering, and memory use can skew the results in favor of what order you run them in.
     
    deathshadow, Jan 5, 2020 IP
    JEET likes this.
  9. JEET

    JEET Notable Member

    Messages:
    2,606
    Likes Received:
    185
    Best Answers:
    4
    Trophy Points:
    215
    #9
    Thanks for replies. Greatly appreciated, helped a lot!
     
    JEET, Jan 7, 2020 IP