Created
August 31, 2018 12:01
-
-
Save franzliedke/6872eabc5ef93dd130b388b268863e02 to your computer and use it in GitHub Desktop.
Benchmark Laravel's pluck() method
This file contains 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 | |
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