Skip to content

Instantly share code, notes, and snippets.

@osmianski
Last active April 13, 2023 11:00
Show Gist options
  • Save osmianski/5ff4e72c6e982b316064fd612edfa86b to your computer and use it in GitHub Desktop.
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/#/>
<?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