开发者

PHP How can I optimise a script?

开发者 https://www.devze.com 2023-03-11 15:44 出处:网络
This script seems to me too long. Is there any way to optimize it? function delteg($string){ $keyp=0; $out=\"\";

This script seems to me too long. Is there any way to optimize it?

function delteg($string){
$keyp=0;
$out="";
   for ($i=0; $i<strlen($string); $i++) { 
           if ($string[$i]=="<"):
             $keyp=1;
           endif;

           if ($keyp==0):
             $out.=$string[$i];
           endif;

           if ($string[$i]==">"):
             $keyp=0;
           endif;

   }
   $out = substr($out,0,strlen($out));
   return $out;
}

$time_step = array();
$datetime = array();
$G = array();
$开发者_JAVA百科HHii = array();
$cloud_cover = array();
$precipitation = array();
$pressure = array();
$temperature = array();
$humidity = array();
$wind_direction = array();
$wind_velocity = array();
$falls = array();
$drops = array();
$n = 0;

$handle = fopen ("http://rp5.ua/xml/257885/en", "r");
while (!feof ($handle)) {
$buffer = fgets($handle, 4096); 

 if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));
 if (strpos($buffer, "point_name_trim")>0) ($point_name_trim[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name2")>0) ($point_name2[$n]=delteg($buffer));
 if (strpos($buffer, "point_timestamp")>0) ($point_timestamp[$n]=delteg($buffer)); 
 if (strpos($buffer, "gmt_add")>0) ($gmt_add[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_date")>0) ($point_date[$n]=delteg($buffer));
 if (strpos($buffer, "point_date_time")>0) ($point_date_time[$n]=delteg($buffer)); 
 if (strpos($buffer, "time_step")>0) ($time_step[$n]=delteg($buffer)); 
 if (strpos($buffer, "datetime")>0) ($datetime[$n]=delteg($buffer));
 if (strpos($buffer, "G")>0) ($G[$n]=delteg($buffer)); 
 if (strpos($buffer, "HHii")>0) ($HHii[$n]=delteg($buffer));
 if (strpos($buffer, "cloud_cover")>0) ($cloud_cover[$n]=delteg($buffer)); 
 if (strpos($buffer, "precipitation")>0) ($precipitation[$n]=delteg($buffer));
 if (strpos($buffer, "pressure")>0) ($pressure[$n]=delteg($buffer)); 
 if (strpos($buffer, "temperature")>0) ($temperature[$n]=delteg($buffer));
 if (strpos($buffer, "humidity")>0) ($humidity[$n]=delteg($buffer)); 
 if (strpos($buffer, "wind_direction")>0) ($wind_direction[$n]=delteg($buffer));
 if (strpos($buffer, "wind_velocity")>0) ($wind_velocity[$n]=delteg($buffer)); 
 if (strpos($buffer, "falls")>0) ($falls[$n]=delteg($buffer));
 if (strpos($buffer, "drops")>0) :($drops[$n]=delteg($buffer));
   $n++;
 endif;

}

fclose ($handle);

for ($i=0; $i<$n; $i++) {
     echo $country_id[$i]." - ".$point_name[$i]. " etc.....";

}


Definitely. You should use an XML parser, like SimpleXML.

Example:

$xml = simplexml_load_file("http://rp5.ua/xml/257885/en");

foreach($xml->point as $point) {
    echo 'Point: ', $point->point_name, PHP_EOL;
    foreach($point->timestep as $step) {
        echo "\t Time: ", $step->datetime, PHP_EOL;
    }
}

There is really no reason to parse XML manually. You save yourself a lot of trouble if you use a parser.


Well some might say use single quotes for strings instead of double quotes as PHP will interpolate the string when using doubles. Ilia has some nice optimisation techniques for general PHP. http://ilia.ws/archives/12-PHP-Optimization-Tricks.html

It all looks pretty fine from my point of view the only thing I would say is, is there any reason why you call strlen() in the for loop declaration, because it would be faster to call it once assign the number to a variable and use that in the for loop.

Oh yeah and use an XML parser, the XML parser object with some callbacks would work and simpleXML is also good.


There are many problems with your script. For example:

  • you are not using XML parser and you just process it as a string (not efficient, basically wrong) - use for example SimpleXML,
  • there are many lines of your code that are not needed, because do not change the results of your code - example solution below,
  • write your code in a consistent way (eg. use indents, use the same notation for conditional clauses) - does not really influence the speed of execution, but will help you with code maintenance,

The problem lies for example here:

if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));
 if (strpos($buffer, "point_name_trim")>0) ($point_name_trim[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name2")>0) ($point_name2[$n]=delteg($buffer));
 if (strpos($buffer, "point_timestamp")>0) ($point_timestamp[$n]=delteg($buffer)); 
 if (strpos($buffer, "gmt_add")>0) ($gmt_add[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_date")>0) ($point_date[$n]=delteg($buffer));
 if (strpos($buffer, "point_date_time")>0) ($point_date_time[$n]=delteg($buffer)); 
 if (strpos($buffer, "time_step")>0) ($time_step[$n]=delteg($buffer)); 
 if (strpos($buffer, "datetime")>0) ($datetime[$n]=delteg($buffer));
 if (strpos($buffer, "G")>0) ($G[$n]=delteg($buffer)); 
 if (strpos($buffer, "HHii")>0) ($HHii[$n]=delteg($buffer));
 if (strpos($buffer, "cloud_cover")>0) ($cloud_cover[$n]=delteg($buffer)); 
 if (strpos($buffer, "precipitation")>0) ($precipitation[$n]=delteg($buffer));
 if (strpos($buffer, "pressure")>0) ($pressure[$n]=delteg($buffer)); 
 if (strpos($buffer, "temperature")>0) ($temperature[$n]=delteg($buffer));
 if (strpos($buffer, "humidity")>0) ($humidity[$n]=delteg($buffer)); 
 if (strpos($buffer, "wind_direction")>0) ($wind_direction[$n]=delteg($buffer));
 if (strpos($buffer, "wind_velocity")>0) ($wind_velocity[$n]=delteg($buffer)); 
 if (strpos($buffer, "falls")>0) ($falls[$n]=delteg($buffer));
 if (strpos($buffer, "drops")>0) :($drops[$n]=delteg($buffer));
   $n++;
 endif;

where you really need only first two lines:

if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));

because later you use only the values generated by them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号