> On Apr 27, 2025, at 07:26, Niels Dossche <dossche.ni...@gmail.com> wrote:
> 
> Regarding performance however, rather than introducing yet another completely 
> new concept to do almost the same thing, why not try to improve exception 
> performance instead?
> 
> I just opened a PR that makes instantiating exceptions much much faster, and 
> this is only after like 15 mins of work. I'm sure there's even more to gain.

I mean, squeeze out gains where you can where the effort:reward ratio is good, 
but the following is a naive but representative result on an MacBook M3 Pro:

```
return false    100000 times = 0.075289011001587
throw exception 100000 times = 0.11530804634094
```

Do we consider a difference of 0.075/100000s vs 0.115/100000s that big a deal 
when compared to (e.g.) establishing a database connection?

This is the naive comparison code; let me know if it's comparing the wrong 
things.

```php
<?php
function returnFalse(int $k)
{
        $before = microtime(true);

        for ($i = 0; $i < $k; $i ++) {
                returnFalse0();
        }

        $duration = microtime(true) - $before;
        echo "return false $k times = " . $duration . PHP_EOL;
}

function returnFalse0() { return returnFalse1(); }
function returnFalse1() { return returnFalse2(); }
function returnFalse2() { return returnFalse3(); }
function returnFalse3() { return returnFalse4(); }
function returnFalse4() { return returnFalse5(); }
function returnFalse5() { return returnFalse6(); }
function returnFalse6() { return returnFalse7(); }
function returnFalse7() { return returnFalse8(); }
function returnFalse8() { return returnFalse9(); }
function returnFalse9() { return false; }

function throwException(int $k)
{
        $before = microtime(true);

        for ($i = 0; $i < $k; $i ++) {
                try {
                        throwException0();
                } catch (Exception $e) {
                }
        }

        $duration = microtime(true) - $before;
        echo "throw exception $k times = " . $duration . PHP_EOL;
}

function throwException0() { throwException1(); }
function throwException1() { throwException2(); }
function throwException2() { throwException3(); }
function throwException3() { throwException4(); }
function throwException4() { throwException5(); }
function throwException5() { throwException6(); }
function throwException6() { throwException7(); }
function throwException7() { throwException8(); }
function throwException8() { throwException9(); }
function throwException9() { throw new Exception(); }

$k = 100000;
returnFalse($k);
throwException($k);

```


-- pmj

Reply via email to