0

I am optimizing a php script for fast performance. The php script read 75 text file line by line and check a line exists in string and do rest

It is a HTTP API script which is called 1000 times per second.

I am creating a function to do this in the api.php script

function searchFile($data, $path)
{
    $result=false;

    if(file_exists($path))
    {
        $data_array= explode("\n", file_get_contents($path));
        foreach($data_array as $data_value)
        {
           if(str_replace(' ', '', $data_value) != '')
           {
               if(stripos($data, $data_value) !== false)
               {
                    $result = true;
                    file_put_contents('log.txt', $data.': '.$path.PHP_EOL, FILE_APPEND);
                    break ;
               }
           }
        }
    }

    return $result;
}

$path_array[1]=.......
$path_array[2]=.......
---
$path_array[75]=.......

$data=..

foreach($path_array as $path)
{
  if(searchFile($data, $path))
  {
    // do rest
  }
}

My Questions..

1) The file size of each file max 500KB, so I use file_get_contents to read fast inserted fopen, fgets,

-- AM I CORRECT?

2) Each file has about 20000 lines. D0 I NEED TO USE unset($data_array) INSIDE FUNCTION FOR BETTER PERFORMANCE?

unset($data_array); 

Thank you in advance

user231410
  • 129
  • 1
  • 15
  • If your code executes as expected and you would like it to be reviewed (scrutinized for possible refinements), I recommend [codereview.se]. – mickmackusa Dec 27 '19 at 21:41
  • Consider using some cache to cache you results (need to profile your requests to see if they have some similarity. – Elias Soares Dec 28 '19 at 01:14
  • Also you probably can use `grep` for check if string is present on the file without loading it in PHP. Probably a lot faster since grep read byte by byte – Elias Soares Dec 28 '19 at 01:17
  • See this answer about grep usage: https://stackoverflow.com/a/9059047/3429323 – Elias Soares Dec 28 '19 at 01:18
  • Thank you for reply I need to search if line exists in the string not string exits in the file I am sorry my English was not clear – user231410 Dec 31 '19 at 04:00
  • It seems to me that you need to have a rethink about how you process this data. Making 20,000 iterated checks per file does not seem like an ideal task. Surely there is a better way. Please provide some sample input and file lines. – mickmackusa Dec 31 '19 at 06:49

0 Answers0