在页面上启动一个大类会减慢速度吗?

发布于 2024-12-27 20:01:16 字数 560 浏览 1 评论 0原文

我正在编写一个类,最终可能会有大约 3000 行代码。

我想知道的很简单,即使只使用一个/两个对象方法,在每个页面顶部启动此类是否会减慢页面的运行时间?如果每天被访问数千次,是否会给我的服务器带来更大的压力?

如果是这样,我是否应该考虑创建扩展来处理每种方法,而不是将整个类放在一个文件中?

已编辑

首先,为了纠正 KingCrunch 和 Kenaniah,此类用于我的 API,这意味着它拥有许多用于检索要在网站和我们的 iPhone 应用程序上显示的数据的函数,以及我们的整个 Facebook 应用程序。考虑到我们网站的规模和功能,3000 行实在是太小了,更不用说其中 700 多行是评论了。所以我可以向你保证,虽然可能存在结构缺陷,但没有设计缺陷,这就是我问这个问题的原因......

构造函数只是将默认值设置为定义的变量,仅此而已。

我已经从头开始完全重写了这个文件,因此没有旧代码,并且我非常确定类中的方法尽可能高效。

我一直在监视我的服务器使用情况等,并使用 apache ab 工具模拟大量流量,尽管我的内存使用量猛增,但似乎确实没问题。

I am in the process of writing a class that will probably end up being around 3000 lines of code.

What I would like to know is very simple, will initiating this class at the top of each page slow down the runtime of the page, even though only one/two of the objects methods will be used? Is it going to put a lot more strain on my server if it is being accessed several thousand times a day?

If so, should I be looking at creating extensions to handle each method instead of having the whole class in one file?

EDITED

Firstly, just to correct KingCrunch and Kenaniah, this class is for my API, which resultantly means that it holds a lot of functions for retrieving data to be displayed on the website and our iPhone Application, along with our entire Facebook Application. So 3000 lines is pretty damn small given the size and capabilities of our website, not to mention that over 700 of these lines are comments. So I can assure you there is no design flaw, although there may be a structural flaw, which is why I am asking this question...

The construct function simply sets the default values to the defined variables, nothing more.

I have completely rewritten this file from scratch so there is no old code and I am pretty sure the methods within the class are as efficient as they can possibly be.

I have been monitoring my server usage etc, as well as simulating high volumes of traffic using the apache ab tool and although my memory usage shoots up, it does seem to be okay.

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

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

发布评论

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

评论(3

庆幸我还是我 2025-01-03 20:01:17

会在每个页面的顶部启动此类,从而减慢页面的运行时间

它会增加运行时间吗?是的。当然。没有什么是免费的。解析的每一行代码都有一些小的开销(但是您可以使用 APC 等操作码缓存消除大部分开销)。然而,我们讨论的开销可能是亚毫秒级的。唯一确定的方法就是亲自进行分析。

如果每天访问数千次,是否会给我的服务器带来更大的压力?

从个人经验来看,不会。但还是要再次对自己进行剖析和衡量。您应该监视服务器上的基本性能指标(CPU 使用率、平均负载等)。部署您的更改并观察您的指标。

will initiating this class at the top of each page slow down the runtime of the page

Will it add to the runtime? Yes. Of course. Nothing is free. Every line of code parsed has some small overhead (however you can get rid of most of this cost with a opcode cache like APC). However, we're talking sub-millisecond overhead, probably. The only way to be sure is to profile it yourself.

Is it going to put a lot more strain on my server if it is being accessed several thousand times a day?

From personal experience, no. But again, profile and measure yourself. You should be monitoring basic performance metrics on your server (CPU usage, load average, etc.). Deploy your change, and watch your metrics.

殤城〤 2025-01-03 20:01:17

不,实例化由大量 LOC 组成的类不会自动使其变慢。

也就是说,除非您在构造函数中执行某些操作,但这取决于您在那里执行的操作,而不是类的大小。

No, instantiating a class that is made up of lots of LOC, does not automatically make it slow.

That is, unless you do something in the constructor, but then it depends on what you are doing there, and not how big the class is.

过期以后 2025-01-03 20:01:17

不,实际上它比将其拆分为多个文件要快。

唯一的问题是,这通常会导致代码块很大并且修改起来更困难。

编辑:如果所有的行都是有用的,它会更快。如果您有很多旧代码,您可能会考虑清理

no, actually it's faster than splitting it in multiple files.

the only problem is that often result in a big block of code and modifications are harder to do.

EDIT: it will be faster if all the lines are usefull. if you have a lot of old code you might consider a clean-up

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