Re: Optimize code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Wiberg wrote:
Hi!

I wonder if I can optimize following code (or do something better) below?
It works fine when about 3.000 rows exists in the textfile, but when it gets
to 10.000 rows then it will get a session timeout. (or internal server
error)

I assume the func gets called for every row in the textfile. If you are not worried about exactly how long it takes then unset the timeout on the script, e.g.:

set_time_limit(0);

and checkout this function also:
http://nl2.php.net/manual/en/function.ignore-user-abort.php



<?php

function checkSaldo($url, $articleNrFieldNr, $saldoFieldNr, $separator,
$startLine, $prefix) {

require ("phpfunctions/opendb.php");

this require() can live outside the function, that saves 3000+ calls to it.


//Read in specified file into array // $fileArray = file($url);

oops! I wasn't reading properly, obviously the function is called once and the iteration happens inside it...

you grab the whole contents of the file at once - potentially storing a 10000
item array in memory, why not check out some of the other 'file' functions
(like fgets(), which allows you to grab 1 line at a time)


//Go through array // for ($i=$startLine;$i<count($fileArray);$i++) {

ARGH!!! at the very least change this line to:

$cFA = count($fileArray);
for ($i=$startLine;$i<$cFA;$i++) {

that will save 3000+ calls to count().


//Get line of file (through array) // $lineRead = $fileArray[$i];


//Make array of all "elements" in this line // $lineArray = explode("$separator",$lineRead);


//Get manufacturers articlenumber // if (isset($lineArray[$articleNrFieldNr])) {

		$articleNumberManufacturer = $lineArray[$articleNrFieldNr];

	}

	else {

		$articleNumberManufacturer = 0;

	}


//Get saldo for this product // if (isset($lineArray[$articleNrFieldNr])) {

		$saldo = $lineArray[$saldoFieldNr];

 	}

 	else {

		$saldo = 0;

	}

	//There is no data on this row
	//set saldo and articlenr to zero, so nothing happens
	//
	if (strlen($lineRead)==0) {

		$saldo = 0;
		$articleNumberManufacturer = 0;

	}


//echo "<br>ARTICLENR: $articleNumberManufacturer<br>"; //echo "SALDO: $saldo<br>";



	//Articlenr exists in line
	//
	if (intval($articleNumberManufacturer)>0) {


//Get ID of product (if there is any in varupiratens db) //skip the product if the saldo is the same as in //the textfile // $sql = "SELECT IDVara FROM tbvara WHERE Varunamn = '$prefix$articleNumberManufacturer' LIMIT 1;"; $querys = mysql_query($sql); $dbArray = mysql_fetch_array($querys); $IDVara = $dbArray["IDVara"]; if ($IDVara == Null) {$IDVara = 0;}

maybe cache this result of this query, assuming it returns repetetive values? although if the cache (array?) grows too large then that might slow it down again... double edged sword.



	//If product is found, then update saldo
	//
	if (intval($IDVara)>0) {

considering the UPDATE query, you can probably drop the call to intval():

if ($IDVara > 0) {


$sql = "UPDATE tbvara SET Saldo=$saldo WHERE IDVara=$IDVara LIMIT 1;";

I don't think the semicolon in the sql statement should be passed according to official docs, although I guess it works anyway :-)

		$querys = mysql_query($sql);
		echo "QUERY DB -> $sql<br>";

also if you are using a very new version of MySQL _I_think_ you have the ability to use prepared queries - i.e. a query where the statement, with some placeholders is compiled once and then you execute the prepared query x number of times each time passing the 'execute()' func the relevant vars. this is as opposed to what you are doing now which is having the query compiled for every line you parse.

prepared queries may not be an option.


}

	//END Articlenr exists in line
	}

}
mysql_close();
}

?>

/G
@varupiraten.se

--
Internal Virus Database is out-of-date.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.1 - Release Date: 2005-01-19


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux