非常慢的PHP脚本

时间:2014-05-15 15:08:21

标签: php performance

当我运行代码时,即使我在下半部分注释我打开同一个文件,它也会非常慢。我已经改变了stream_get_line的长度。文件是64MB,73500行,159列。任何优化方面的帮助都会非常感激。

<?php

   include "configure.php";
   $row = 1;
   if(($handle = fopen("ACC_half.txt", "r")) !== false)
   {
      global $arraySamples;
      global $arrayTrans;
      global $num;
      global $lines;
      while (!feof($handle))
      {    
         $data = stream_get_line($handle, 1000000, "\n");
         $num = count($data);
         $lines = count(file("ACC_half.txt")); 
         //Get all the names of the samples into array
         if($row == 1)
         {
            for($i=0;$i<$num;$i++)
            {
               //echo $data[$i];
               $arraySamples[]=$data[$i];
            }
            //Get all the names of the first row(headers) into array for use 
         } 
         else
         { 
            $arrayTrans[]=$data[0];
         }
         $row++; 
      }
   }
   fclose($handle);
   $row = 1;
   $transInc=1;
   if(($handle1 = fopen("ACC_half.txt", "r")) !== false)
   {
      while (($data2 = stream_get_line($handle,1000000,"\n\r")) !== false)
      {
         if(($row == 2) || ($row==1))
         {
            $row++;continue;
         }
         $jnum=count($data2);
         for($j=1;$j<$jnum;$j+=2)
         {
            $g=$j+1;
            $h=$j+2;
            $import = mysql_query("INSERT into acc (form,sample,raw,scale) 
                  VALUES ('$arrayTrans[$transInc]', '$arraySamples[$g]', '$data2[$j]', '$data2[$g]')") 
               or die (mysql_error());  
         }
         $transInc++;
      }
   }
   fclose($handle1);

?>

1 个答案:

答案 0 :(得分:1)

我认为其中一个主要问题是你打开两次相同的大文件。你不应该打开这个文件一次