PHP, fread reading slow?

Friday, November 10, 2006 18:03
Posted in category PHP
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

I have a bunch of php scripts running on my cacti servers for the company parsing data from routers and creating graphs, recently i was struggling again with a custom made PHP script for cacti which would count pppoe sessions per VLAN.
I had problems with the telnet script (in php) running way to slow, i was trying to figure out why, untill i started re-reading the code, everything seemed ok, i started searching around the net for a possible solution, writting in a different language was out of the question, since i am strong with PHP and i didn’t have any time to do so.

I search untill i read the small snip of my code:

{
      $r .= fread($this->fp, 1000);
      $s = socket_get_status($this->fp);
} while ($s['unread_bytes']);

It seemed ok…but it just seemed, I though that reading smaller chunks would be quicker, but hell i was wrong…that was my problem, changing the file buffer from 1000 to 8000:

{
      $r .= fread($this->fp, 8000);
      $s = socket_get_status($this->fp);
} while ($s['unread_bytes']);

solved all my problems, the time that took the script to execute with 1000 bytes buffer was around 1m and 4secs, with the buffer set to 8000 the execution time dropped to 0m and 4secs! 1 minute difference…

  • Share/Save/Bookmark
You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.