Jump to content

Recommended Posts

Hi all,

 

i have the following code but it is very slow it takes 411 seconds to deal with 1000 lines of data, the data that im dealing with has lines of more than 100,000 so i want to try and cut down the execution time.

 

Does any one have any ideas as to how i might be able to achieve this?

 

Thanks in advance, Phil

 

function replace_inarray(&$str, $key, $replace) {

foreach ($replace as $k=>$v) {
$check = explode("|", $v);
if ($check[1] != 1) {
if ($str == $k) {
$str = str_replace($k, $v, $str);
}
}

else {
if (strpos($str, $k)) {
$str = str_replace($k, $check[0], $str);
}
}
}
}
function array_cycle(&$array, $callback, $thirdparam=NULL) {
foreach ($array as $k=>$v) {
	if ($thirdparam != NULL) {
	$array[$k] = $callback($v, $thirdparam);
	}
	else {
	$array[$k] = $callback($v);
	}
}
}

function return_elem($array, $key) {
return $array[$key];
}




$to_rep = array_keys($arr);

		if (in_array($data[2], $to_rep) && (in_array($data[3], $to_rep) && (in_array($data[5], $to_rep) || empty($data[5])))) {

		array_walk($data, "replace_inarray", $arr);
		array_cycle($data, "mysql_real_escape_string");
		echo "yes $data[2]|$data[3]|$data[5]<br />";	
		}
		else {
			echo "no $data[2]|$data[3]|$data[5]<br />";
			$data[2] = @mysql_real_escape_string($data[2]);
			$data[3] = @mysql_real_escape_string($data[3]);
			$data[5] = @mysql_real_escape_string($data[5]);

			array_walk($data, "replace_inarray", $arr);
		}

 

Link to comment
https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/
Share on other sites

It takes the data from a csv and does the following things..

 

it replaces the data from the csv with data that has been loaded into an array, and then inserts the replaced data into a deals table, however for this to happen two columns have to have the data replaced and the third column is optional.

 

If this makes sense?

 

So in the example i gave data3 & data5 need to be replaced for it to be inserted into the deals table data6 is optional. (although the code i uploaded doesn't have the insert code)

 

If 3 & 5 both arent matched or any arent matched the whole row of the csv are then placed into a error table.

 

so for example if 3 & 6 are not matched the whole row of the csv goes into a error table and so on.

 

After further checking it seems to be the array walk func that is causing it to be slow.

 

function array_cycle(&$array, $callback, $thirdparam=NULL) {
foreach ($array as $k=>$v) {
	if ($thirdparam != NULL) {
	$array[$k] = $callback($v, $thirdparam);
	}
	else {
	$array[$k] = $callback($v);
	}
}
}

 

I hope this all makes sense?

 

Thanks for your help

 

Phil

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.