I'll lay out my whole issue here just in case I'm incorrect about where the problem is.
I wrote some software in PHP to build a quote for a building company. I have a manual quantity calculation for every product they have in inventory and each time they modify the inputs, like the building dimensions, it recreates the quote and spits out a new price based on the quantity calculations for each individual product being used.
There are a number of inputs on the page and I make an AJAX call to the server to recreate the quote, then display the new quote once the AJAX return is done. This includes running all the quantity calculations.
When requesting the quantities, it goes into a switch statement consisting of 300 cases until it finds the matching ID of the product being requested. It then runs the quantity calculation I have coded in there which may or may not rely on quantities of other products in the system as well.
I have statements at the top and bottom of the switch statement to check an overall array to see if that item ID is already in the overall array (if the quantity has been calculated already). If so, it returns that quantity instead of recalculating it. If not, at the bottom of the switch statement it adds that quantity to the overall array in case that product quantity is requested again.
Here's how it looks:
function getQuantityBuildingProduct($productId, $showInsulation, $showPerma)
{
$quantities = array();
if (isset($this->overallQuantities[$productId]))
return $this->overallQuantities[$productId];
switch($productId)
{
case ($this->cornerMoulding) :
$quantity = 0;
if ($this->isInsulated() || $showInsulation)
{
$quantity += ceil(
$this->getTotalQuantityBuildingProduct($this->threeByThreeFixedWindow, $showInsulation, $showPerma) * 1.5 +
$this->getTotalQuantityBuildingProduct($this->fourByThreeFixedWindow, $showInsulation, $showPerma) * 1.5;
);
$quantity += $this->getTotalQuantityBuildingProduct($this->threeFootStandardDoor, $showInsulation, $showPerma) * 2.5;
}
$quantities[] = ceil($quantity);
break;
case ($this->smokeDamper) :
$quantities[] = ceil(($this->getTotalQuantityBuildingProduct($this->smokeStop, $showInsulation, $showPerma) + $this->getTotalQuantityBuildingProduct($this->fireStop, $showInsulation, $showPerma)) * 3);
break;
case ($this->tuckTape) :
$quantities[] = ceil($this->getTotalQuantityBuildingProduct($this->poly, $showInsulation, $showPerma) / 2000);
break;
case ($this->ceilingOnlyLabour) :
if ($this->isCeilingOnly())
$quantities[] = 1;
break;
default:
break;
}
$this->overallQuantities[$productId] = $quantities;
return $quantities;
}
It's taking about 10 seconds to get the entire quote back from the server since I implemented these switch statements. I have others to also get the widths, heights and lengths of the products just like I get these quantities. I used to have a separate function to get every single quantity, width, length and height but that was a lot of individual functions so I found this easier to add new functions to. Is this the cause my the slowdown? How can I make it faster?
My dream goal is to somehow store all of the quantity calculations in the database but I have no idea how I can do that since they're all different and sometimes rely on quantities of other products. I think having them in the database may speed things up (or slow them down) but I'm just not sure how to accomplish that right now. For now I'd just like to speed up the current way.