Welcome Guest, Not a member yet? Register   Sign In
supressing errors is really slow
#1

[eluser]xwero[/eluser]
I've did a quick and dirty benchmark
Code:
$end = 10;
$this->benchmark->mark('dog');
for($i=0;$i<$end;$i++)
{
  $value = @$_POST['test'];
}
$this->benchmark->mark('cat');
for($i=0;$i<$end;$i++)
{
  $value = (isset($_POST['test']))?$_POST['test']:'';
}
$this->benchmark->mark('bird');
        
echo '@ time : '.$this->benchmark->elapsed_time('dog', 'cat').'<br>';
echo 'if time: '.$this->benchmark->elapsed_time('cat', 'bird').'<br>';

1 time result
@ time : 0.0001
if time: 0.0000

10 times result
@ time : 0.0003
if time: 0.0000

100 times result
@ time : 0.0023
if time: 0.0001

1000 times result
@ time : 0.0226
if time: 0.0014

There is quite some suppressing in CI's code so i start to wonder is it really necessary or is there alternative, faster code?
#2

[eluser]webthink[/eluser]
That's a pretty severe slowdown you're showing there. 16 times slower over 1000 cycles even though you've added a conditional. I did a quick google and can't find any resources that talk about this. (It's a php issue as well as a CI issue so you'd think there would be some information out there)
I'm all for speeding things up regardless of how small the gain may be.
I think in general using the @operator is not good practice and should be avoided.
I haven't had a chance to look for any of these instances in the CI code could you point me in the direction of a few examples? Are they mostly like your example where they're used to avoid a notice from using a bad array index?
#3

[eluser]xwero[/eluser]
The database drivers all suppress the function errors. I'm not sure if there is code like my example, but i remember there was one library where i saw it too be i can't remember which one it was.
#4

[eluser]xwero[/eluser]
I'm not sure if this is a solution but if you define an error_reporting level in the bootstrap file and in the database driver methods you do
Code:
function db_connect()
    {
       error_reporting(0);
           return mysqli_connect($this->hostname, $this->username, $this->password);
           error_reporting(ERRORLEVEL);
    }
#5

[eluser]m4rw3r[/eluser]
function_exists()?
Code:
function db_connect()
{
    if(function_exists('mysqli_connect') && $ret = mysqli_connect($this->hostname, $this->username, $this->password))
        return $ret;
    else
        // raise some errors here
}
#6

[eluser]xwero[/eluser]
No function_exist doesn't stop the errors from appearing. That is the problem. Even if you have an error handler the error gets shown.
#7

[eluser]xwero[/eluser]
Benchmarks on the error_reporting solution
Code:
$end = 1000;
        $this->benchmark->mark('dog');
        for($i=0;$i<$end;$i++)
        {
            $value = @$_POST['test'];
        }
        $this->benchmark->mark('cat');
        for($i=0;$i<$end;$i++)
        {
            error_reporting(0);
            $value = $_POST['test'];
            error_reporting(E_ALL);
        }
        $this->benchmark->mark('bird');
        
        echo '@ time : '.$this->benchmark->elapsed_time('dog', 'cat').'<br>';
        echo 'error_reporting time: '.$this->benchmark->elapsed_time('cat', 'bird').'<br>';

1 time result
@ time : 0.0001
error_reporting time: 0.0000

10 times result
@ time : 0.0003
error_reporting time: 0.0003

100 times result
@ time : 0.0022
error_reporting time: 0.0027

1000 times result
@ time : 0.0222
error_reporting time: 0.0266

so that's even worse than suppressing errors.
#8

[eluser]xwero[/eluser]
I've made a modification of the last snippet and put the error_reporting functions outside the loop and it got me this result when looped 1000 times

@ time : 0.0222
error_reporting time: 0.0191

Are there benchmark nuts here who can tell if these numbers actually matter?
#9

[eluser]m4rw3r[/eluser]
Does mysqli_connect raise PHP errors?
I thought it only raised errors if it doesn't exists (which is very disturbing, no explanation why the db doesn't work, only a blank page).
#10

[eluser]xwero[/eluser]
yes database functions all throw a E_WARNING error if a database error occurs, that is why they are suppressed.




Theme © iAndrew 2016 - Forum software by © MyBB