Skip to content

Instantly share code, notes, and snippets.

@franzliedke
Created August 31, 2018 12:01
Show Gist options
  • Save franzliedke/6872eabc5ef93dd130b388b268863e02 to your computer and use it in GitHub Desktop.
Save franzliedke/6872eabc5ef93dd130b388b268863e02 to your computer and use it in GitHub Desktop.
Benchmark Laravel's pluck() method
<?php
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'makroskop',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'mawp_',
]);
$capsule->setAsGlobal();
function benchmark($name, $callback) {
echo "BENCHMARK $name\n";
$start_time = microtime(true);
for ($i = 0; $i < 1000; $i++) {
$callback();
}
$end_time = microtime(true);
echo ($end_time - $start_time)."\n";
}
benchmark('pluck-new', function() {
Capsule::table('posts')->pluck('post_title', 'id');
});
benchmark('pluck-old', function() {
Capsule::table('posts')->oldPluck('post_title', 'id');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment