Last active
April 13, 2023 11:00
-
-
Save osmianski/5ff4e72c6e982b316064fd612edfa86b to your computer and use it in GitHub Desktop.
Benchmarking resolving an instance from the Laravel container against raw `new` operator. Run this here: <https://laravelplayground.com/#/>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Test | |
{ | |
} | |
Route::get('/', function (){ | |
$output = ''; | |
$count = 10000; | |
$startedAt = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
new Test(); | |
} | |
$output .= sprintf("new operator: %fms\n", $elapsed1 = (microtime(true) - $startedAt) * 1000 / $count); | |
$startedAt = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
app(Test::class); | |
} | |
$output .= sprintf("app(): %fms\n", $elapsed2 = (microtime(true) - $startedAt) * 1000 / $count); | |
$output .= sprintf("%f times slower\n", $elapsed2 / $elapsed1); | |
return "<code><pre>$output</pre></code>"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment