php – try catch is faster than @?

Question:

try {
include('');
} catch(e) {
 die('error');
}

or

@include('') or die('error');

or

if (file_exists('')) {
 include('');
}

or

$result = ('SELECT 1 FROM files WHERE file_name="" LIMIT 1');
if ($result) {
 include('');
}

Answer:

Correct. The dog does not get rid of the error, it only suppresses its output.

And try-catch allows you not only to suppress, but also to take some specific measures. the speed is very funny. If everything goes smoothly, this is the fastest way, aside from just bare code. One of the slowest when an error occurs.

<?php
$time = microtime(true);
for($i=0;$i<1000000;$i++){
    @divByZero($i,0);
}
print((microtime(true)-$time).'<br />');
//1.66923713684 - делим на 0
//1.25963785172 - делим на 3

$time = microtime(true);
for($i=0;$i<1000000;$i++){
    try{
        divByZeroE($i,0);
    }catch(Exception $e){

    }
}
print((microtime(true)-$time).'<br />');
//5.61499404907 - делим на 0
//0.690489969254 - делим на 3

$time = microtime(true);
for($i=0;$i<1000000;$i++){
    divByZeroIfOnly($i,0);
}
print((microtime(true)-$time).'<br />');
//0.69531083107 - делим на 0
//0.69578036312 - делим на 3

function divByZero($a,$b){
    return $a/$b;
}

function divByZeroE($a,$b){
    if($b==0)
        throw new Exception('Division By Zero');
    return $a/$b;
}

function divByZeroIfOnly($a,$b){
    if($b==0)
        return false;
    return $a/$b;
}

now full? =)

Scroll to Top