Skip to main content
deleted 754 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Running a loop on a .dat file in miminmuma minimum possible time in PHP

Hello friends. I am using the following code to run an operation on a .dat file, which is working fine. The .dat file can be treated as a text file. My main concern is how to improveimproving execution time when you have to run a for loop on 25 Mb of data.

There isare no problemproblems in the above code, except that the .dat file I am using is 25 Mb, so you can understand it will take a long, long time to run the above scriptit. How can I decrease the execution time? Can I use indexing or what? Please help

 // echo '<pre>';
 //print_r($lat_lon_sunshines);
  //echo '<pre>';count($lat_lon_sunshines);

$closest = 500;
for($c=0;$c<count($lat_lon_sunshines);$c++)
{

//echo $c;  //echo $c;
$lat2=$lat_lon_sunshines[$c]['lat'];
 $lan2=$lat_lon_sunshines[$c]['lan'];
$sunshines=$lat_lon_sunshines[$c]['sunshine'];
//print_r($lat_lon_sunshines$sunshines);die;
//die;
 $lat_diff = abs(round((float)($lat1), 4)-$lat2);

if ($lat_diff < $closest)
{
    $diff = //echo$lat_diff count+  abs($lat_lon_sunshinesround((float)($lan1), 4)-$lan2); 
    
     if($diff < $closest)
     {
              $closest = 500;$diff;
    for($c=0;$c<count($lat_lon_sunshines);$c++)        $sunshinesfinal=$sunshines;
     }      
}
$sunshines='';

}
//echo 'sumit';
 print_r($sunshinesfinal);die;

function make_sunshine_dict($file_name)
{
    
 $sunshines_dict = array(); //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
   $f = $lan2=$lat_lon_sunshines[$c]['lan'];file_get_contents($file_name);
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
  $handle = //print_rfopen($sunshines$file_name, "r");die;;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
   
  if/* for($lat_diff < $closest$kkk=0;$kkk<100;$kkk++)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
       $buffer = iffgets($diff < $closest$handle)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';;*/
    
  while($buffer = fgets($handle))
  {
 
  
 // print_r($buffer);die;
  $tok = strtok($buffer, " \n\t");
  
   $lat=$tok;
   
   $latArray[]=$tok;
  
  //$lat=round(float(values[0]), 4)
  
 //print_r($buffer);
  
 
  
 // print_r($tok);
  
   $tok = strtok(" \n\t");
   
   $months = '';
   
   $months = array();
   
    //print_r($tok);die;
   for ($k = 0; $tok !== false; $k+=1) 
   {
   
   if($k==0)
   {
   $lan=$tok;
   $lanArray[]=$tok;
   }
   
   if($k!=0)
   {
  /// echo $tok;
        $months[] = $tok ;
         "month $k : ".$months[$k]."<br>";
            
   }
     
    $tok = strtok(" \n\t");
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function 
 make_sunshine_dict   /*echo $lat;
    echo '<br>';
    echo $lang;
    echo '<br>';
    print_r($file_name$months);*/
    
    
    //$data=new stdClass();
    /*$data->sunshine[]->lat=$lat;
    $data->sunshine[]->lan=$lang;*/
    
    $data[$kkk]['lat']=$lat;
    $data[$kkk]['lan']=$lan;
    //$sunshines=array();
    
    foreach($months as $m=>$sunshine)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              /  $sunshinesdata=(($sumD/print_r30)*($sunshinesfloat);die;($sunshine)*.01);
              
              
              
  $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
        
          //echo '<pre>';
           
    $sunshines='';      //print_r($sunshines);die;
        }  
        //echo '<pre>'; 
        //print_r($data);   $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
           
        }
  $sunshines='';
   //die; }
    return//echo $data;'<pre>';
    //print_r($data);
    
    }
 //die;
return $data;
    
}

Running loop on .dat file in miminmum possible time in PHP

Hello friends. I am using the following code to run an operation on a .dat file, which is working fine. The .dat file can be treated as a text file. My main concern is how to improve execution time when you have to run a for loop on 25 Mb of data.

There is no problem in the above code, except that the .dat file I am using is 25 Mb so you can understand it will take a long, long time to run the above script. How can I decrease the execution time? Can I use indexing or what? Please help

    // echo '<pre>';
     //print_r($lat_lon_sunshines);
      //echo count($lat_lon_sunshines);
    
    $closest = 500;
    for($c=0;$c<count($lat_lon_sunshines);$c++)
    {
    
    //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
     $lan2=$lat_lon_sunshines[$c]['lan'];
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
    //print_r($sunshines);die;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
    if ($lat_diff < $closest)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
         if($diff < $closest)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';
    
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function make_sunshine_dict($file_name)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              //print_r($sunshines);die;
              
              
               $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
                
              $sunshines='';
        }
        //echo '<pre>';
        //print_r($data);
        
        }
     //die;
    return $data;
        
    }

Running a loop on a .dat file in a minimum possible time

I am using the following code to run an operation on a .dat file, which is working fine. The .dat file can be treated as a text file. My main concern is improving execution time when you have to run a for loop on 25 Mb of data.

There are no problems in the above code, except that the .dat file I am using is 25 Mb, so you can understand it will take a long, long time to run it. How can I decrease the execution time? Can I use indexing or what?

// echo '<pre>';
 //print_r($lat_lon_sunshines);
  //echo count($lat_lon_sunshines);

$closest = 500;
for($c=0;$c<count($lat_lon_sunshines);$c++)
{

//echo $c;  //echo $c;
$lat2=$lat_lon_sunshines[$c]['lat'];
 $lan2=$lat_lon_sunshines[$c]['lan'];
$sunshines=$lat_lon_sunshines[$c]['sunshine'];
//print_r($sunshines);die;
//die;
 $lat_diff = abs(round((float)($lat1), 4)-$lat2);

if ($lat_diff < $closest)
{
    $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
    
     if($diff < $closest)
     {
              $closest = $diff;
            $sunshinesfinal=$sunshines;
     }      
}
$sunshines='';

}
//echo 'sumit';
 print_r($sunshinesfinal);die;

function make_sunshine_dict($file_name)
{
    
 $sunshines_dict = array();     
 
  $f = file_get_contents($file_name);
  
  $handle = fopen($file_name, "r");
  
   
    
 
  /* for($kkk=0;$kkk<100;$kkk++)
   {    
   
   $buffer = fgets($handle);*/
    
  while($buffer = fgets($handle))
  {
 
  
 // print_r($buffer);die;
  $tok = strtok($buffer, " \n\t");
  
   $lat=$tok;
   
   $latArray[]=$tok;
  
  //$lat=round(float(values[0]), 4)
  
 //print_r($buffer);
  
 
  
 // print_r($tok);
  
   $tok = strtok(" \n\t");
   
   $months = '';
   
   $months = array();
   
    //print_r($tok);die;
   for ($k = 0; $tok !== false; $k+=1) 
   {
   
   if($k==0)
   {
   $lan=$tok;
   $lanArray[]=$tok;
   }
   
   if($k!=0)
   {
  /// echo $tok;
        $months[] = $tok ;
         "month $k : ".$months[$k]."<br>";
            
   }
     
    $tok = strtok(" \n\t");
    }
    
     
    /*echo $lat;
    echo '<br>';
    echo $lang;
    echo '<br>';
    print_r($months);*/
    
    
    //$data=new stdClass();
    /*$data->sunshine[]->lat=$lat;
    $data->sunshine[]->lan=$lang;*/
    
    $data[$kkk]['lat']=$lat;
    $data[$kkk]['lan']=$lan;
    //$sunshines=array();
    
    foreach($months as $m=>$sunshine)
    {
    
    
         $sunshines=array();
          $sumD = 0;
          
        // $iteration= ($m+1)*30+1;
          
          $iteration= 31;
          
          for($n=1;$n<=$iteration;$n++)
          {
              $J = ($m+1)*$n; 
             
            
             
             $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
             
            
             
          $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
             
            /* $value ? ($value > 1 and 1) : $value;
            
             $value ? ($value < -1 and -1): $value;*/
             
             
          $D = 24 - ((24/pi) * acos($value));
            
            
             
           $sumD = $sumD + $D;
            
             }
              
                $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
              
              
              
          
          //echo '<pre>';
           
          //print_r($sunshines);die;
          
          
           $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
           
          $sunshines='';
    }
    //echo '<pre>';
    //print_r($data);
    
    }
 //die;
return $data;
    
}

running Running loop on .dat file in miminmum possible time in phpPHP

Hello Friends I am using the following code to run opertaion on the .dat file which is working fine.The .dat file can be treted as a text file .My main concern is how to improve execution time when you have to run a for loop on 25mb of data
    
    
    $file_name='grid_10min_sunp.dat';
    
    $handle = fopen($file_name, "r");
    
    $lat1=13.86082;
    $lan1=100.50509;
    
     $lat_lon_sunshines = make_sunshine_dict($file_name);

Hello Friendsfriends. I am using the following code to run opertaionan operation on thea .dat file, which is working fine. The .dat file can be treated as a text file. My main concern is how to improve execution time when you have to run a for loop on 25 Mb of data.

There is no problem in the above code, except that the .dat file iI am using is of 25mb25 Mb so you can understand it will take a long -long, long time to run abocethe above script.How How can i decreseI decrease the execution time.can i? Can I use indexing or what? Please help

    // echo '<pre>';
     //print_r($lat_lon_sunshines);
      //echo count($lat_lon_sunshines);
    
    $closest = 500;
    for($c=0;$c<count($lat_lon_sunshines);$c++)
    {
    
    //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
     $lan2=$lat_lon_sunshines[$c]['lan'];
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
    //print_r($sunshines);die;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
    if ($lat_diff < $closest)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
         if($diff < $closest)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';
    
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function make_sunshine_dict($file_name)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              //print_r($sunshines);die;
              
              
               $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
               
              $sunshines='';
        }
        //echo '<pre>';
        //print_r($data);
        
        }
     //die;
    return $data;
        
    }

There is no problem in above code except that the .dat file i am using is of 25mb so you can understand it will take a long -long time to run aboce script.How can i decrese the execution time.can i use indexing or what?
Please help 

running loop on .dat file in miminmum possible time in php

Hello Friends I am using the following code to run opertaion on the .dat file which is working fine.The .dat file can be treted as a text file .My main concern is how to improve execution time when you have to run a for loop on 25mb of data
    
    
    $file_name='grid_10min_sunp.dat';
    
    $handle = fopen($file_name, "r");
    
    $lat1=13.86082;
    $lan1=100.50509;
    
     $lat_lon_sunshines = make_sunshine_dict($file_name);

Hello Friends I am using the following code to run opertaion on the .dat file which is working fine

There is no problem in above code except that the .dat file i am using is of 25mb so you can understand it will take a long -long time to run aboce script.How can i decrese the execution time.can i use indexing or what? Please help

    // echo '<pre>';
     //print_r($lat_lon_sunshines);
      //echo count($lat_lon_sunshines);
    
    $closest = 500;
    for($c=0;$c<count($lat_lon_sunshines);$c++)
    {
    
    //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
     $lan2=$lat_lon_sunshines[$c]['lan'];
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
    //print_r($sunshines);die;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
    if ($lat_diff < $closest)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
         if($diff < $closest)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';
    
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function make_sunshine_dict($file_name)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              //print_r($sunshines);die;
              
              
               $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
               
              $sunshines='';
        }
        //echo '<pre>';
        //print_r($data);
        
        }
     //die;
    return $data;
        
    }

There is no problem in above code except that the .dat file i am using is of 25mb so you can understand it will take a long -long time to run aboce script.How can i decrese the execution time.can i use indexing or what?
Please help 

Running loop on .dat file in miminmum possible time in PHP

Hello friends. I am using the following code to run an operation on a .dat file, which is working fine. The .dat file can be treated as a text file. My main concern is how to improve execution time when you have to run a for loop on 25 Mb of data.

There is no problem in the above code, except that the .dat file I am using is 25 Mb so you can understand it will take a long, long time to run the above script. How can I decrease the execution time? Can I use indexing or what? Please help

    // echo '<pre>';
     //print_r($lat_lon_sunshines);
      //echo count($lat_lon_sunshines);
    
    $closest = 500;
    for($c=0;$c<count($lat_lon_sunshines);$c++)
    {
    
    //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
     $lan2=$lat_lon_sunshines[$c]['lan'];
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
    //print_r($sunshines);die;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
    if ($lat_diff < $closest)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
         if($diff < $closest)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';
    
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function make_sunshine_dict($file_name)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              //print_r($sunshines);die;
              
              
               $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
               
              $sunshines='';
        }
        //echo '<pre>';
        //print_r($data);
        
        }
     //die;
    return $data;
        
    }
Source Link

running loop on .dat file in miminmum possible time in php

Hello Friends I am using the following code to run opertaion on the .dat file which is working fine.The .dat file can be treted as a text file .My main concern is how to improve execution time when you have to run a for loop on 25mb of data
    
    
    $file_name='grid_10min_sunp.dat';
    
    $handle = fopen($file_name, "r");
    
    $lat1=13.86082;
    $lan1=100.50509;
    
     $lat_lon_sunshines = make_sunshine_dict($file_name);

Hello Friends I am using the following code to run opertaion on the .dat file which is working fine

$file_name='grid_10min_sunp.dat';

$handle = fopen($file_name, "r");

$lat1=13.86082;
$lan1=100.50509;

 $lat_lon_sunshines = make_sunshine_dict($file_name);
 

// echo '<pre>';
 //print_r($lat_lon_sunshines);
  //echo count($lat_lon_sunshines);

$closest = 500;
for($c=0;$c<count($lat_lon_sunshines);$c++)
{

//echo $c;  //echo $c;
$lat2=$lat_lon_sunshines[$c]['lat'];
 $lan2=$lat_lon_sunshines[$c]['lan'];
$sunshines=$lat_lon_sunshines[$c]['sunshine'];
//print_r($sunshines);die;
//die;
 $lat_diff = abs(round((float)($lat1), 4)-$lat2);

if ($lat_diff < $closest)
{
    $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
    
     if($diff < $closest)
     {
              $closest = $diff;
            $sunshinesfinal=$sunshines;
     }      
}
$sunshines='';

}
//echo 'sumit';
 print_r($sunshinesfinal);die;

function make_sunshine_dict($file_name)
{
    
 $sunshines_dict = array();     
 
  $f = file_get_contents($file_name);
  
  $handle = fopen($file_name, "r");
  
   
    

  /* for($kkk=0;$kkk<100;$kkk++)
   {    
   
   $buffer = fgets($handle);*/
    
  while($buffer = fgets($handle))
  {
 
  
 // print_r($buffer);die;
  $tok = strtok($buffer, " \n\t");
  
   $lat=$tok;
   
   $latArray[]=$tok;
  
  //$lat=round(float(values[0]), 4)
  
 //print_r($buffer);
  
 
  
 // print_r($tok);
  
   $tok = strtok(" \n\t");
   
   $months = '';
   
   $months = array();
   
    //print_r($tok);die;
   for ($k = 0; $tok !== false; $k+=1) 
   {
   
   if($k==0)
   {
   $lan=$tok;
   $lanArray[]=$tok;
   }
   
   if($k!=0)
   {
  /// echo $tok;
        $months[] = $tok ;
         "month $k : ".$months[$k]."<br>";
            
   }
     
    $tok = strtok(" \n\t");
    }
    
    
    /*echo $lat;
    echo '<br>';
    echo $lang;
    echo '<br>';
    print_r($months);*/
    
    
    //$data=new stdClass();
    /*$data->sunshine[]->lat=$lat;
    $data->sunshine[]->lan=$lang;*/
    
    $data[$kkk]['lat']=$lat;
    $data[$kkk]['lan']=$lan;
    //$sunshines=array();
    
    foreach($months as $m=>$sunshine)
    {
    
    
         $sunshines=array();
          $sumD = 0;
          
        // $iteration= ($m+1)*30+1;
          
          $iteration= 31;
          
          for($n=1;$n<=$iteration;$n++)
          {
              $J = ($m+1)*$n; 
             
            
             
             $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
             
            
             
          $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
             
            /* $value ? ($value > 1 and 1) : $value;
            
             $value ? ($value < -1 and -1): $value;*/
             
             
          $D = 24 - ((24/pi) * acos($value));
            
            
             
           $sumD = $sumD + $D;
            
             }
              
                $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
              
              
             
          
          //echo '<pre>';
          
          //print_r($sunshines);die;
          
          
           $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
           
          $sunshines='';
    }
    //echo '<pre>';
    //print_r($data);
    
    }
 //die;
return $data;
    
}

There is no problem in above code except that the .dat file i am using is of 25mb so you can understand it will take a long -long time to run aboce script.How can i decrese the execution time.can i use indexing or what? Please help

    // echo '<pre>';
     //print_r($lat_lon_sunshines);
      //echo count($lat_lon_sunshines);
    
    $closest = 500;
    for($c=0;$c<count($lat_lon_sunshines);$c++)
    {
    
    //echo $c;  //echo $c;
    $lat2=$lat_lon_sunshines[$c]['lat'];
     $lan2=$lat_lon_sunshines[$c]['lan'];
    $sunshines=$lat_lon_sunshines[$c]['sunshine'];
    //print_r($sunshines);die;
    //die;
     $lat_diff = abs(round((float)($lat1), 4)-$lat2);
    
    if ($lat_diff < $closest)
    {
        $diff = $lat_diff +  abs(round((float)($lan1), 4)-$lan2); 
        
         if($diff < $closest)
         {
                  $closest = $diff;
                $sunshinesfinal=$sunshines;
         }      
    }
    $sunshines='';
    
    }
    //echo 'sumit';
     print_r($sunshinesfinal);die;
    
    function make_sunshine_dict($file_name)
    {
        
     $sunshines_dict = array();     
     
      $f = file_get_contents($file_name);
      
      $handle = fopen($file_name, "r");
      
       
        
    
      /* for($kkk=0;$kkk<100;$kkk++)
       {    
       
       $buffer = fgets($handle);*/
        
      while($buffer = fgets($handle))
      {
     
      
     // print_r($buffer);die;
      $tok = strtok($buffer, " \n\t");
      
       $lat=$tok;
       
       $latArray[]=$tok;
      
      //$lat=round(float(values[0]), 4)
      
     //print_r($buffer);
      
     
      
     // print_r($tok);
      
       $tok = strtok(" \n\t");
       
       $months = '';
       
       $months = array();
       
        //print_r($tok);die;
       for ($k = 0; $tok !== false; $k+=1) 
       {
       
       if($k==0)
       {
       $lan=$tok;
       $lanArray[]=$tok;
       }
       
       if($k!=0)
       {
      /// echo $tok;
            $months[] = $tok ;
             "month $k : ".$months[$k]."<br>";
                
       }
         
        $tok = strtok(" \n\t");
        }
        
        
        /*echo $lat;
        echo '<br>';
        echo $lang;
        echo '<br>';
        print_r($months);*/
        
        
        //$data=new stdClass();
        /*$data->sunshine[]->lat=$lat;
        $data->sunshine[]->lan=$lang;*/
        
        $data[$kkk]['lat']=$lat;
        $data[$kkk]['lan']=$lan;
        //$sunshines=array();
        
        foreach($months as $m=>$sunshine)
        {
        
        
             $sunshines=array();
              $sumD = 0;
              
            // $iteration= ($m+1)*30+1;
              
              $iteration= 31;
              
              for($n=1;$n<=$iteration;$n++)
              {
                  $J = ($m+1)*$n; 
                 
                
                 
                 $P = asin(.39795*cos(.2163108 + 2*atan(.9671396*tan(.00860*($J-186)))));
                 
                
                 
              $value=(sin(0.8333*pi/180) + sin($lat*pi/180)*sin($P))/(cos($lat*pi/180)*cos($P));
                 
                /* $value ? ($value > 1 and 1) : $value;
                
                 $value ? ($value < -1 and -1): $value;*/
                 
                 
              $D = 24 - ((24/pi) * acos($value));
                
                
                 
               $sumD = $sumD + $D;
                
                 }
                  
                    $sunshinesdata=(($sumD/30)*(float)($sunshine)*.01);
                  
                  
                 
              
              //echo '<pre>';
              
              //print_r($sunshines);die;
              
              
               $data[$kkk]['sunshine'][$m]=$sunshinesdata; 
               
              $sunshines='';
        }
        //echo '<pre>';
        //print_r($data);
        
        }
     //die;
    return $data;
        
    }

There is no problem in above code except that the .dat file i am using is of 25mb so you can understand it will take a long -long time to run aboce script.How can i decrese the execution time.can i use indexing or what?
Please help