Skip to content

Instantly share code, notes, and snippets.

@jlpoveda
Created August 24, 2015 22:22
Show Gist options
  • Save jlpoveda/9b1188eb364f9bfd2100 to your computer and use it in GitHub Desktop.
Save jlpoveda/9b1188eb364f9bfd2100 to your computer and use it in GitHub Desktop.
PDO rendimiento
<?php
class User {
private $matricula;
private $marca_id;
public function __construct($matricula, $marca_id)
{
$this->matricula = $matricula;
$this->marca_id = $marca_id;
}
public function display() {
$cosa = 1;
echo $this->matricula . ' -- ' . $this->marca_id . "\r";
}
}
function metodoAntiguo(&$sq) {
$result = $sq->fetchAll();
$collection = array();
foreach($result as $r) {
$collection[] = new User($r['matricula'], $r['marca_id']);
}
foreach ($collection as $r) {
$r->display();
}
}
function metodoNuevo(&$sq) {
$result = $sq->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'User', array(null, null));
foreach ($result as $r) {
$r->display();
}
}
$dns = 'mysql:host=localhost;dbname=pruebas';
$user = 'root';
$pass = '';
$db = new PDO($dns, $user, $pass);
/*
for($i = 1; $i < 5000; ++$i) {
$sql = "INSERT INTO stock_tienda (matricula, marca_id) VALUES ('AAA" . $i . "', " . rand(1, 500) . ")";
$db->exec($sql);
}
*/
$sql = "SELECT matricula, marca_id FROM stock_tienda";
$total_time = 0;
if($argv[1] == 1) {
echo "Metodo Antiguo" . PHP_EOL;
} elseif($argv[1] == 2) {
echo "Metodo Nuevo" . PHP_EOL;
} else {
die('Error... Necesito un argumento');
}
for($i = 1; $i <= 20; $i++) {
$sq = $db->query($sql);
$time_start = microtime(true);
if($argv[1] == 1) {
metodoAntiguo($sq);
} elseif($argv[1] == 2) {
metodoNuevo($sq);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
$total_time = $total_time + $time;
}
echo ($total_time / $i) . " segundos de media\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment