Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fastest method of reading server response

I'm having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls I'm making.

I'm sending through a batch of say, 10,000 calls and I've tracked the lag down to fgets causing the hold up in the speed of my application as the response for each call needs to be grabbed.

I have found this thread http://bugs.php.net/bug.php?id=32806 which explains the problem quite well, but he's reading a file, not a server response so fread could be a bit tricky as I could get part of the next line, and extra stuff which I don't want.

So my question is, what is the best/fastest way to read the response from the server as an alternative to fgets?

like image 344
Peter John Avatar asked Apr 19 '10 20:04

Peter John


1 Answers

file_get_contents (or stream_get_contents if you have a stream) should be the fastest way to read a server's response. Well, its the fastest way to retrieve data, but often it is the most wasteful way when looking at memory usage, since it reads all of the file at once into memory while fgets does not need to keep more than one line in memory.

You use fread as well, which is faster than fgets and which reads the file in chunks of a specific size that you can define.

If you depend on reading data linewise, you can use file() which will be slower than file_get_contents, but which gives you an array with the lines of the file.

To give you a better answer -as already mentioned above-, we need more information.

like image 184
yankee Avatar answered Oct 03 '22 01:10

yankee