Skip to content

Instantly share code, notes, and snippets.

@avoelkl
Last active August 17, 2016 11:54
Show Gist options
  • Save avoelkl/660cae48ef87e3f133ccbc587c4fa342 to your computer and use it in GitHub Desktop.
Save avoelkl/660cae48ef87e3f133ccbc587c4fa342 to your computer and use it in GitHub Desktop.
Testing efficient way of loading customer data
<?php
require_once '../app/Mage.php';
Mage::app();
umask(0);
$customerId = 575420;
$start = microtime();
$memory1 = memory_get_usage();
$customer = Mage::getModel('customer/customer')->load($customerId);
$firstname = $customer->getFirstname();
$lastname = $customer->getLastname();
$memory2 = memory_get_usage();
$end = microtime();
echo "<p>";
echo $firstname . "<br />";
echo $lastname . "<br />";
echo "Start: $start, End: $end => " . ($end - $start) . "<br />";
echo "Memory Start: $memory1, End: $memory2 => " . ($memory2 - $memory1)/1024 . "KB <br />";
echo "</p><p>";
$start = microtime();
$memory3 = memory_get_usage();
$customer = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToFilter('entity_id', $customerId)
->getFirstItem();
$firstname = $customer->getFirstname();
$lastname = $customer->getLastname();
$memory4 = memory_get_usage();
$end = microtime();
echo "<p>";
echo $firstname . "<br />";
echo $lastname . "<br />";
echo "Start: $start, End: $end => " . ($end - $start) . "<br />";
echo "Memory Start: $memory3, End: $memory4 => " . ($memory4 - $memory3)/1024 . " KB<br />";
echo "</p>";
@avoelkl
Copy link
Author

avoelkl commented Aug 17, 2016

First call: 0.027145
Second call: 0.0021870000000001

@avoelkl
Copy link
Author

avoelkl commented Aug 17, 2016

First call:
Time: 0.018482
Memory: 1875.171875KB

Second call:
Time: 0.001501
Memory: 122.3671875 KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment