Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Created April 7, 2015 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstuifzand/0b5a4a10a417d03ad107 to your computer and use it in GitHub Desktop.
Save pstuifzand/0b5a4a10a417d03ad107 to your computer and use it in GitHub Desktop.
Magento database collection speed tests
<?php
include_once('app/Mage.php');
Mage::app('default');
function makeWalker($collection, $modelName) {
///echo $collection->getSelect(). "\n";
return array($collection->getSelect()->query(), $modelName);
}
function fetch($walker, $model = null) {
$row = $walker[0]->fetch();
if (!$row) return false;
if (!$model) {
$model = Mage::getModel($walker[1]);
}
$model->setData($row);
return $model;
}
$start = memory_get_usage();
$time_start = microtime(true);
for ($i = 0; $i < 500; ++$i) {
$collection = Mage::getModel('catalog/product')->setStoreId(1)->getCollection()
->addAttributeToSelect('*');
$iterator = makeWalker($collection, 'catalog/product');
$product = null;
while ($product = fetch($iterator, $product)) {
# echo $product->getName() . "\n";
# echo $product->getPrice() . "\n";
}
}
$end = memory_get_usage();
$diff = $end - $start;
echo "$start\n";
echo "$end\n";
echo "$diff\n";
$time_end = microtime(true);
$time_diff = $time_end - $time_start;
echo "$time_diff seconds\n";
<?php
include_once('app/Mage.php');
Mage::app('default');
$start = memory_get_usage();
$time_start = microtime(true);
for ($i = 0; $i < 500; ++$i) {
$collection = Mage::getModel('catalog/product')->setStoreId(1)->getCollection()
->addAttributeToSelect('*');
foreach ($collection as $product) {
# echo $product->getName() . "\n";
# echo $product->getPrice() . "\n";
}
}
$end = memory_get_usage();
$diff = $end - $start;
echo "$start\n";
echo "$end\n";
echo "$diff\n";
$time_end = microtime(true);
$time_diff = $time_end - $time_start;
echo "$time_diff seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment