5

What is benefit and difference between the following:

Statement 1:

header("Content-type: image/jpeg");
header('Expires: ' . date('r',time() + 864000));
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
$splitString = str_split($contents, 1024);
foreach($splitString as $chunk)
echo $chunk;

Statement 2:

header("Content-type: image/jpeg");
header('Expires: ' . date('r',time() + 864000));
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
echo $contents;
shamittomar
  • 46,210
  • 12
  • 74
  • 78
Chetan Sharma
  • 2,539
  • 5
  • 25
  • 41

4 Answers4

3

Due to the way TCP/IP packets are buffered, using echo to send large strings to the client may cause a severe performance hit. Sometimes it can add as much as an entire second to the processing time of the script. This even happens when output buffering is used.

If you need to echo a large string, break it into smaller chunks first and then echo each chunk. So, using method 1 to split the string OR using substr to split it and sending to client performs faster for large files than method 2.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 1
    pretty sure PHP/Apache are capable of handling this without help from PHP programmers... – sbeam Jan 04 '11 at 05:23
  • 1
    @sbeam, I have personally tested it and found it better. Maybe some configurations of Web-server and PHP may do it automagically, but when you don't know which server is going to be used (IIS, Apache, Lighthttpd, etc), it's better to optimize your own script. – shamittomar Jan 04 '11 at 05:25
  • wow. any webserver out there is capable of handling the TCP packet construction without your help, these things are not written by idiots who don't know about TCP/IP (even IIS). Whatever benchmark you did was flawed somehow. This is a useless micro-optimization that doesn't even do anything except muddle the code. – sbeam Jan 04 '11 at 05:49
  • 1
    @sbeam, I would advise you to please run a test yourself in some spare time and see the difference yourself. – shamittomar Jan 04 '11 at 05:54
  • @shamittomar this might make echo _seem_ faster, but it doesn't actually free resources. Solution is to tune your webserver http://fplanque.com/dev/linux/why-echo-is-slow-in-php-how-to-make-it-really-fast – sbeam Jan 04 '11 at 06:01
  • 1
    @sbeam, yes I agree. BUT, what about all the **shared** webhostings where you can't change settings of your web-server ? – shamittomar Jan 04 '11 at 06:03
0

Statement 1 adds unnecessary overhead and obfuscation.

sbeam
  • 4,622
  • 6
  • 33
  • 43
0

in the first statement content divide into 1024 bytes chunks and one chunk have 1024 bytes content and in the second statement detemine the length of whole content ant then echo this but in first divide in chunk and then echo with help of for each one by one.

pooja
  • 2,334
  • 3
  • 16
  • 16
0

If the $contents is very large, you can echo it in chunks to stop the whole thing being echo'd at once.

alex
  • 479,566
  • 201
  • 878
  • 984