PHP 打印大量文本时速度很慢

发布于 2024-12-06 12:45:13 字数 682 浏览 2 评论 0原文

我制作了一个 ajax 应用程序,该应用程序在我的本地开发服务器上运行得很好,但是当我将其转移到网上时,一个特定的请求变得非常慢。这个请求非常复杂 - 它从数据库加载大量内容并创建相当大的文本输出,大约120kB。由于该应用程序编写时间较短,因此还有很大的优化空间。当然,我试图找到最慢我的应用程序的原因 - 我很惊讶 - 这是最后的最终回声,它正在打印所有计算的信息。

我使用Firebug来测量时间。该请求花费了 ~100ms 而不打印信息,只是计算,但是 ~400ms 进行了打印......所以简单的 echo 命令花费了大约 300ms! > 然后我尝试了PHP的microtime()来获得更精确的结果...但是突然打印和不打印之间没有显着差异。所以我猜问题出在其他地方 - 在将文本发送到 Apache 然后发送到客户端的区域......我不明白这些东西,但我在某处读到这可能是由 Apache 缓冲区较小引起的。我可以做点什么吗?我不认为 120kB 太多 - 就在几年前,在表格布局的时代,大多数大型网站都有这种大小的 html 源。

这可能是虚拟主机的问题吗?我可以尝试联系他们,但如果我自己能解决这个问题自然会更容易。

I made an ajax application, which worked great on my local dev server, but when I moved it online, one particular request got really slow. This request is quite complicated - it loads a lot of stuff from database and creates quite big text output, around 120kB. Since the app was written in a short time, there was a lot of space for optimization. Naturally I was trying to find what's slowing my app most - and I was surprised - it was the last final echo which was printing all calculated information.

I used Firebug to measure the times. The request took ~100ms without printing the info, just calculating, but ~400ms with printing... so the simple echo command took around 300ms! Then I tried PHP's microtime() to get more precise results... but suddenly there was no significant difference between printing and not printing. So I guess the problem is somewhere else - in the area of sending text to Apache and then to the client... I don't understand this stuff, but I read somewhere that this might be caused by small Apache buffer. Can I do something about it? I don't think that 120kB is too much - just few years ago, in the time of table layouts, most of the big websites had html source of this size.

Could this be a problem of the webhosting? I could try to contact them but naturally it would be easier if I could solve this myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-12-13 12:45:14

答案很简单。我的下载速度约为 300kB/s,因此下载 120kB 页面的速度不会超过约 300 毫秒。就这些了,我真的很蠢:D

The answer is really simple. My download speed is around 300kB/s so downloading a 120kB page can't be faster than ~300ms. That's all and I'm really stupid :D

夜无邪 2024-12-13 12:45:14
ob_start();
echo $huge_string;
ob_end_flush();
ob_start();
echo $huge_string;
ob_end_flush();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文