php - 输出文件时 "echo" block 有什么好处?

标签 php performance file

什么是优点和以下区别:

语句 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;

声明 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;

最佳答案

由于 TCP/IP 数据包的缓冲方式,使用 echo 向客户端发送大字符串可能会严重影响性能。有时它可以使脚本的处理时间增加整整一秒。使用输出缓冲时甚至会发生这种情况。

如果您需要回显一个大字符串,请先将其分成较小的 block ,然后回显每个 block 。 因此,对于文件,使用方法 1 拆分字符串或使用 substr 拆分字符串并发送到客户端比方法 2 执行得更快。

关于php - 输出文件时 "echo" block 有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4590848/

相关文章:

.net - 为什么 regex.IsMatch(str) 比 str.EndsWith(不变文化)快?

excel - 有没有办法将范围批量写入文本/CSV 文件?

php - 为什么 PDO query() 结果的键包含表名?

PHP:按单词和标签将字符串准确拆分为数组

php - JavaScript 计数与 PHP 循环一致

java - io异常 : a required privilege is not held by client while writing in file in java

PHP 表单不上传文件

php - php中MongoGridFS::put和MongoGridFS::storeFile函数有什么区别?

jquery - 当您快速移动鼠标时,鼠标移动速度会变慢

c++ - 用一个 int 替换多个 bool 并使用位掩码的性能优势?