I have an custom attribute in my products that I put the discount amount in for filtering on the front end. So I created a small script that calculates the discount amount and saves for each product if it differs (so if a price is changed without changing the discount amount this will rectify it once a night). It runs through 14000 products so when there is nothing to update it takes about 2 minutes to run, however when there are products to update (first time run updated about 5000 products and took about 17 minutes).
are there any changes I can make that might streamline and get the run time down on this?
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$_products_collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*');
$time_start = microtime(true);
foreach($_products_collection as $_product)
{
$_disc_value = ceil(($_product->getPrice() - $_product->getSpecialPrice()) / $_product->getPrice() * 100);
if($_product->getDiscount_int() == $_disc_value):
;
else:
$_product->setDiscount_int($_disc_value)
->save();
;
endif;
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo 'Total Execution Time: '.$execution_time.' Seconds';
echo "\n";
?>`