如何加速 1800 行 PHP 包含?它使我的页面加载速度减慢至 10 秒/视图

发布于 2024-08-31 20:40:49 字数 846 浏览 2 评论 0原文

我设计的代码将所有重要函数放在一个 PHP 文件中,该文件现在有 1800 行长。

我在其他 PHP 文件(例如 AJAX 处理器)中使用简单的“require_once("codeBank.php")”来调用它。

我发现加载所有这些函数大约需要 10 秒,尽管我只涉及一些全局数组和一堆其他函数。例如,主要的 AJAX 处理器代码仅进行一个简单的语法验证就需要 8 秒(其操作函数存储在 codeBank.php 中)。

当我注释掉 require_once 时,我的 AJAX 响应时间从 10 秒加快到 40 毫秒,因此很明显 PHP 正在尝试用这 1800 行函数做一些事情。即使安装了 APC,情况也是如此,这令人惊讶。

我应该怎样做才能让我的代码速度回到 100 毫秒以下的水平?我是否无法以某种方式获得缓存的好处?我需要将单一功能库文件切成不同的部分吗?我还可以做其他一些微妙的事情来搞乱我的响应时间吗?

或者除此之外,有哪些工具可以进一步挖掘哪些 PHP 操作遇到了减速带?

==========================

[编辑] 已解决。

==========================

正如许多好心人所指出的,没有逻辑原因为什么仅仅拥有一个 1800 行的 php 函数库就会导致放缓。实际发生的情况是,我有一条调试线正在调用较长的 API 调用函数之一。每当我包含 PHP 文件时,我都是从远程查询数据构建整个数据结构。

当我杀掉那条线后,一切都回到了 30 毫秒的快速响应。对我来说仍然奇怪的是,每次调用 AJAX 脚本时 require_once() 都会打开 php 文件。但这是我的状态不佳,忘记了每次 AJAX 脚本完成时它都会关闭,并且每次都会重新打开和重新编译。

I designed my code to put all important functions in a single PHP file that's now 1800 lines long.

I call it in other PHP files--AJAX processors, for example--with a simple "require_once("codeBank.php")".

I'm discovering that it takes about 10 seconds to load up all those functions, even though I have nothing more than a few global arrays and a bunch of other functions involved. The main AJAX processor code, for example, is taking 8 seconds just to do a simple syntax verification (whose operational function is stored in codeBank.php).

When I comment out the require_once, my AJAX response time speeds up from 10sec to 40ms, so it's pretty clear that PHP's trying to do something with those 1800 lines of functions. That's even with APC installed, which is surprising.

What should I do to get my code speed back to the sub-100ms level? Am I failing to get the cache's benefit somehow? Do I need to cut that single function bank file into different pieces? Are there other subtle things that I could be doing to screw up my response time?

Or barring all that, what are some tools to dig further into which PHP operations are hitting speed bumps?

==========================

[EDIT] Resolved.

==========================

As many of you kind people have noted, there's no logical reason why just having a 1800 line php function library should cause slowdowns. What was actually happening is that I had a debug line that was invoking one of the longer, API-calling functions. Whenever I was including the PHP file, I was constructing a whole data structure from remote, queried data.

Once I killed that line, everything went back to snappy 30ms responses. What remains odd to me is that require_once() opens the php file every time the AJAX script was getting called. But that's me being out of shape and forgetting that every time the AJAX script is finished it closes and is getting reopened and recompiled each time.

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

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

发布评论

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

评论(6

差↓一点笑了 2024-09-07 20:40:49

您可能拥有一个包含 100 000 行代码的文件,但加载时间仍然不会超过 10 秒。

其中可能有一些您没有意识到正在运行的初始化代码。在进行优化之前,查看一下分析器(xdebug 或 Zend Studio 中的分析器)并准确找出导致速度变慢的原因。如果您认为这只是因为文件有 1800 行长,那么您就错了。

You could have a file with 100 000 lines of code and it still shouldn't take 10 seconds to load.

There's probably some initialization code in there that you don't realize is running. Take a look at a profiler (xdebug or the one in Zend Studio) and find out exactly what's causing a slow down before you go down the route of optimization. If you think it's simply because the file is 1800 lines long, you're on the wrong track.

孤星 2024-09-07 20:40:49

确保那里没有 sleep() 函数:)
还可以在处理完文件后使用 apc 来缓存文件,如果 apc 缓存文件时仍然很慢,则问题出在其他地方。我已经包含了 20,000 行的文件,没有任何问题。

make sure there isn't a sleep() function in there :)
also use apc to cache the file after it's been processed, if it's still slow when apc is caching the file then the problem lies elsewhere. I've included files with 20,000 lines with no problems.

骷髅 2024-09-07 20:40:49

您能否将这些函数分组为较小的文件并按需加载?

它们是全局函数还是类,可能具有静态方法?

如果是这样,您可以从 PHP 的自动加载中受益。查看__autoload(),或者如果需要更灵活的解决方案,请查看spl_autoload_register()

Can you group the functions into smaller files and load on demand?

Are they global functions or classes, maybe with static methods?

If so, you could benefit from PHP's autoloading. Look into __autoload() or for a more flexible solution, look at spl_autoload_register().

洒一地阳光 2024-09-07 20:40:49

无论如何,一定要拆分该文件 - 如果没有其他事情,您从使代码库更有组织性中获得的生产力收益将是值得的。

Definitely split that file up regardless - if nothing else the productivity gains you'll get from having your codebase more organized will be worth it.

疯狂的代价 2024-09-07 20:40:49

令人非常惊讶的是,一个包含(即使是 1800 行)可能会减慢这么多。首先,也许您应该检查代码以确保它没有执行不必要的处理。实际上解析文件不应该成为这样的瓶颈。

也就是说,包含是许多 PHP 程序的关键瓶颈之一。您可以考虑将代码重构为许多较小的文件(其中只包含您需要的文件),或者(可能更容易)使用 PHP 的 __autoload 函数。它是一个函数,每当您引用不存在的类时都会自动调用它,因此您需要将函数放入静态类中。例如:

// include/ajaxlib.php
class AjaxLib {
    public static function renderResponse($data) {
        // whatever
    }
}

// a common include file:
function __autoload($class) {
    require 'include/' . strtolower($class) . '.php';
}

// your code:
AjaxLib::renderResponse($foo);

It is very surprising that an include (even of 1800 lines) could slow it down that much. Firstly, perhaps you should look over the code to make sure it's not performing unnecessary processing. Actually parsing the file shouldn't be such a bottleneck.

That said, includes are one of the key bottlenecks in a lot of PHP programs. You could consider refactoring your code either into a number of smaller files (of which you would only include what you need), or (probably much easier), use PHP's __autoload function. It is a function which is automatically called whenever you refer to a class which doesn't exist, therefore you'd need to put your functions into a static class. eg:

// include/ajaxlib.php
class AjaxLib {
    public static function renderResponse($data) {
        // whatever
    }
}

// a common include file:
function __autoload($class) {
    require 'include/' . strtolower($class) . '.php';
}

// your code:
AjaxLib::renderResponse($foo);
酷到爆炸 2024-09-07 20:40:49

我无法想象 1800 行文件会成为问题,我的所有页面中包含的众多类中只有一个 1870 行,它永远不会引起问题。您所包含的文件中是否有任何文件或网络访问权限?

I can't imagine an 1800 line file being a problem, I have just one of the many classes that I include in all of my pages that is 1870 lines, it never causes a problem. Is there any file or network access in the file you are including?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文