iceblox Posted November 14, 2009 Share Posted November 14, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/ Share on other sites More sharing options...
mrMarcus Posted November 14, 2009 Share Posted November 14, 2009 what are you trying to do with this data? Quote Link to comment https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/#findComment-957505 Share on other sites More sharing options...
iceblox Posted November 14, 2009 Author Share Posted November 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/#findComment-957517 Share on other sites More sharing options...
iceblox Posted November 17, 2009 Author Share Posted November 17, 2009 Any ideas how to speed this up? Its certainly the str_replace that is causing it to go so slow but can think how to get it to go quicker.. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/#findComment-959105 Share on other sites More sharing options...
JonnoTheDev Posted November 17, 2009 Share Posted November 17, 2009 You aren't executing this through a web browser are you? Quote Link to comment https://forums.phpfreaks.com/topic/181510-how-to-speed-up-my-code/#findComment-959283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.