Skip to content

Instantly share code, notes, and snippets.

@boussou
Forked from stephen-hill/pdo-fetch-benchmark.php
Last active August 29, 2015 14:02
Show Gist options
  • Save boussou/0741b7a4be02c49c5471 to your computer and use it in GitHub Desktop.
Save boussou/0741b7a4be02c49c5471 to your computer and use it in GitHub Desktop.
<?php
//here is my version (working on a sqlite db)
set_time_limit(0);
class models {
public $year_model ,
$brand ,
$model ,
$mec ,
$finition ,
$cv ,
$energy ,
$argus ,
$argus_good_state ,
$date_reference ,
$properties ,
$info ,
$keywords ,
$problem ,
$id ;
}
// List of fetch styles to test
$fetchStyles = array(
'Assoc' => PDO::FETCH_ASSOC,
'Both' => PDO::FETCH_BOTH,
'Lazy' => PDO::FETCH_LAZY,
'Num' => PDO::FETCH_NUM,
'Obj' => PDO::FETCH_OBJ,
'Class' => PDO::FETCH_CLASS
);
$path="/path/to/sqlite/file";
// PDO connection
$pdo = new PDO
(
// 'mysql:dbname=mysql;host=localhost',
'sqlite:'.$path.'/models.db3',
'benchmark', // Username
'benchmark', // Password
array(
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_PERSISTENT => true
)
);
// The results array
$result = array();
// Loop each style
foreach($fetchStyles as $key => $style)
{
// Total time for each style
$total = 0.0;
for ($i = 0; $i < 100; $i++)
{
// Execute
$statement = $pdo->prepare('SELECT * FROM models');
$statement->execute();
// Record the start time
$start = microtime(true);
if($key=='Class')
for ($i = 0; $i < 10000; $i++)
{
$mixed = $statement->fetchObject("models");
}
else
for ($i = 0; $i < 10000; $i++)
{
$mixed = $statement->fetch($style);
}
// Record the end time
$stop = microtime(true);
// Convert Start and Stop to Miliseconds
$start = $start * 1000.0;
$stop = $stop * 1000.0;
// Calculate the time taken
$diff = ($stop - $start);
// Add the difference to the total
$total = $total + $diff;
}
// Store the result
$result[$key] = $total;
}
// Sort the results
asort($result, SORT_NUMERIC);
// Output the result
// Time taken is in Milliseconds
var_dump($result);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment