Jump to content

Need help to speedup my code please


hyeteck

Recommended Posts

hey guys, i have a program that i wrote to add products to my database from a tab delimited text file.

 

There are 2 text files, an "approved" text file and an "all" text file.  It read the approved text file first and loads all the product ID into an array.  Then it goes through every product in the "all" file and if the product id exists in the approved array of elements, it will create the product.

 

It gives a timeout error after creating 1,500 products.  The total number of products that need to be created is about 16,000.  If i remove the whole deal with the "approved" text file and just have it create all the products in the "all" file then it creates 15,000 products.  So my problem is when im searching the "approved" array of elements.  How can i speed this process up or make it more efficient?

 

here is my code:

<?php

print date("h:i:s A");
echo "<br>";

include "../vsadmin/db_conn_open.php";

echo "stage1<br>";

$dbl_approved = @fopen("dbl_approved_items.txt", "r");
if($dbl_approved)
{	
$index = 0;
$string = fgets($dbl_approved, 4096);
while (!feof($dbl_approved))
{
	$string = fgets($dbl_approved, 4096);
	$chunks = explode("	", $string);

	$approvedList[$index] = $chunks[0];
	$index = $index + 1;
}
}

echo "stage2<br>";
$dblall = @fopen("dbl_all.txt", "r");
if($dblall)
{
$all_string = fgets($dblall, 4096);
while (!feof($dblall))
{
	$all_string = fgets($dblall, 4096);
	$all_chunks = explode("	", $all_string);

	$prodId = $all_chunks[0];
	$catName = $all_chunks[10];

	if((searchArr($approvedList, $prodId))!=-1)
	{
		$result = mysql_query("SELECT `pName` from `products` WHERE `pID`='$prodId'") or die(mysql_error());
		$result2 = mysql_query("SELECT `sectionID` from `sections` WHERE `sectionName`='$catName' AND `rootSection`='1'") or die(mysql_error());

		if(((mysql_num_rows($result))==0) && ((mysql_num_rows($result2))>0))
		{
			$cat_array = mysql_fetch_array($result2);
			$catId = $cat_array['sectionID'];
			$prodWeight = $all_chunks[19];


			if($prodWeight!=0 && $prodWeight<=65)
			{		
				$prodPrice = 0;
				$prodBrandt = $all_chunks[6];
				$prodBrandt = addslashes($prodBrandt);
				$prodBrandi = $all_chunks[7];
				$prodBrandi = "prodimages/brands/".strtolower($prodBrandi);
				$prodName = $all_chunks[11]; 
				$prodName = addslashes($prodName);
				$prodDesc = $all_chunks[12];
				$prodDesc = addslashes($prodDesc);
				$prodDescl = $all_chunks[13];
				$prodDescl = addslashes($prodDescl);
				$prodUPC = $all_chunks[15];
				$prodImage = $all_chunks[20];
				$prodImagel = $all_chunks[20];

				if($prodImage==0 || $prodImage==NULL)
				{
					$prodImagel = "prodimages/large/".strtolower($prodId).".jpg";
					$prodImage = "prodimages/small/".strtolower($prodId).".jpg";
				}
				else
				{
					$prodImagel = "prodimages/large/".strtolower($prodImage);
					$prodImage = "prodimages/small/".strtolower($prodImage);
				}

				$temp = mysql_query("INSERT INTO `products`(pID, pName, pSection, pDescription, pLongdescription, pImage, pLargeimage, pBrandimage, pBrandtext, pPrice, pWeight, pInStock, pDropship, pUPC) VALUES('$prodId', '$prodName', '$catId', '$prodDesc', '$prodDescl', '$prodImage', '$prodImagel', '$prodBrandi', '$prodBrandt', '0', '$prodWeight', '0', '1', '$prodUPC')") or die(mysql_error());
			}
		}		
	}
}
fclose($dblall);
}

function searchArr($arr, $item)
{
$result = -1;

for ($i = 0; $i < count($arr); $i++)
{
	if (strcmp($arr[$i], $item) == 0) 
	{
		$result = $i;
	}
}
return($result);
}
print date("h:i:s A");
echo "<br>DONE!";
?>

Link to comment
https://forums.phpfreaks.com/topic/39079-need-help-to-speedup-my-code-please/
Share on other sites

ok i guess the in_array method is much faster because it runs all the way without a timeout.

 

i did the following

 

if((in_array($prodId, $approvedList))==TRUE)

 

instead of

if((searchArr($approvedList, $prodId))!=-1)

 

and also did an fclose($dbl_approved); after i was done with the "approved" file.

 

is there any other way to optimize my code and make it quicker or more efficient?

string functions are faster than explode(), so I would use that for your first file read. If you want example of what I mean, just tell me.

 

 

Secondly, writing out variables that you just don't need to be writing/copying is wasteful...

 

// wasteful (processing and memory usage)

 

$catId = $cat_array['sectionID'];

 

You do that bunch, if you already have $cat_array['sectionID'], what's the point of copying it to another variable. I see people do that to much, it fine for limited visited sites, but the more busy the site, it starts adding up, very quickly, plus it's bad coding design.

 

printf

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.