Month: February 2014

What is Fast ?PHP Frameworks or Core PHP Functions

SpeedHi Guys

I have been working these days on my site performance in magento. While working and writing code I always have two options ? Either to go with core PHP functions or with the framework functions. However both have their PROS & CONS

Take a simple example to get the image dimensions in magento. (It is improves site performance when you display Image Dimensions with image tag)

So there are two option to get the image dimension

OPTION 1 Through the core PHP function
list($img_width, $img_height, $type, $attr) = getimagesize($_imgUrl);
The time taken to carry out this function was calculated using

$startTime = microtime(true);
list($img_width, $img_height, $type, $attr) = getimagesize($_imgUrl);
echo "


WITH CORE PHP FUNCTION Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";

The output was time taken in loading the image was 0.00003

OPTION 2 With the Magento function

$dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imagePath,'/media'));

/* getting image width and height*/
if (file_exists($dirImg)) {
$startTime = microtime(true);
$imageObj = new Varien_Image($dirImg);
$width = $imageObj->getOriginalWidth();
$height = $imageObj->getOriginalHeight();
echo "
WITH MAGRNTO FUNCTION Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";

echo "
".$width." x ".$height;
}
else {
echo "File doesn't exist.";
}

When the above code was used just to create the image object and get the width and height,

the time taken was WITH MAGRNTO FUNCTION Time: 0.0005 Seconds

This was because we created the Image object “$imageObj = new Varien_Image($dirImg);”

if we ignore the time taken in creating the image object and then see the speed by modifying code as $imageObj = new Varien_Image($dirImg);
$startTime = microtime(true);
$width = $imageObj->getOriginalWidth();
$height = $imageObj->getOriginalHeight();
echo "
WITH MAGRNTO FUNCTION Time: " . number_format(( microtime(true) - $startTime), 14) . " Seconds\n";

The time taken was WITH MAGENTO FUNCTION WHEN NOT CREATING OBJECT Time: 0.00001788139343 Seconds

Thus here time was very less as compared to original php function.

Thus we can say that when you are creating object of any class, it holds all the properties. However if you just need only one information try to use the core php functions as they are quick like in above example.