php - PHP 的 file_get_contents 内存和数据效率高吗?

标签 php file-get-contents vps

我正在制作一个推送通知服务器,它从外部(第三方)html 页面收集特定数据,如果我知道我需要的信息在第一个内,例如5000 个字符,如果我声明 MAX_LENGTH,PHP 实际上会使用更少的内存吗?还是整个页面都完全加载到内存中了?此外,是否下载了整个 html 页面,或者一旦达到限制,连接是否断开? (进而节省数据传输成本)

$html = file_get_contents ("http://.....", false, null, -1, 5000);

谢谢。

最佳答案

是的,它确实节省了内存和带宽......我还进行了速度测试(这与这个问题并不完全相关,但很有用,并建议它停止读取流)和内存测试只是为了演示.我没有运行峰值内存测试,但至少你的 $html 变量将存储更少的信息并在那里节省内存。

Time to get ALL characters of remote page 10 times: 6.0368211269379
Time to get ALL characters of remote page 10 times: 6.0158920288086
Time to get ALL characters of remote page 10 times: 5.8945140838623
Time to get ALL characters of remote page 10 times: 8.867082118988
Time to get ALL characters of remote page 10 times: 5.7686760425568

Time to get first ten characters of page 10 times: 4.2118229866028
Time to get first ten characters of page 10 times: 4.5816869735718
Time to get first ten characters of page 10 times: 4.2146580219269
Time to get first ten characters of page 10 times: 4.1949119567871
Time to get first ten characters of page 10 times: 4.1788749694824

Memory Useage First 10 characters:40048
Memory Useage ALL characters:101064

关于php - PHP 的 file_get_contents 内存和数据效率高吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713922/

相关文章:

php - LEFT OUTER JOIN SUM 双倍问题

php - 使用php从文本文件中删除文本

php - 文件获取内容(): php_network_getaddresses: getaddrinfo failed: Name or service not known

php - 下载前检查远程服务器上是否存在图像

linux - 将 PHP 框架放在 Linux 主机上的什么位置

php - 如何在 laravel 5 Controller 中复制文件

php - php和mysql的问题

elasticsearch - 在不同的VPS上设置ElasticSearch集群

php - CI更新批处理;连接并递增变量和字符串作为数组值

.htaccess 重定向主域而不是子文件夹