Java 对象创建与字符串解析

发布于 2024-10-19 09:19:09 字数 373 浏览 2 评论 0原文

我正在努力将一个以 JSP 为中心的项目迁移到使用 Velocity。在许多地方,JSP 页面只是简单地解析字符串来显示各种内容。这使得JSP明显变得非常丑陋并且难以维护。

我已经修改了此类的控制器来为我完成这项工作(即创建 POJO 列表),然后使用速度迭代各个部分。我有预感,这将会带来很大的阻力。

我意识到创建对象会带来开销,但它使我们的页面更​​容易调试、编写和使用。除此之外,它将 UI 与后台发生的核心逻辑分开。更不用说我们的应用程序服务器很无聊。数据库痛苦地皱起眉头。我们将看到用户数量增加十倍(这可能就是为什么它最初被开发来解析 JSP 中的字符串并跳过对象创建)——这对我来说是过早优化的味道。

有哪些进一步的论据来支持我的主张,即我们应该承担与对象创建相关的成本?

I'm working on migrating a project which is very JSP-centric to using Velocity. In many places the JSP pages were simply parsing strings to display various things. This makes the JSP very ugly and difficult to maintain obviously.

I have modified the Controller for this class to do this work for me (i.e. creating a List of POJOs), then iterating over the pieces using velocity. I have a feeling that it's going to come with heavy resistance.

I realize that creating the Object comes with overhead, but it makes our pages much easier to debug, write and use. Besides the fact it separates the UI from the core logic of what is occurring in the background. Not to mention our appservers are BORED. The database is wincing in pain. We are to see a tenfold increase in users (which could be why it was initially developed to parse the strings in the JSP & skip the object creation) -- this smells to me of premature optimization.

What are further arguments to support my claim that we should eat the cost associated with the object creation?

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

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

发布评论

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

评论(3

月朦胧 2024-10-26 09:19:09
  • 大约 15 年前,Java 中的对象创建有点昂贵。从那时起,Java 运行时有了很大的改进。如今,Java 对象创建通常比 C++ 更快。
  • 从未如此昂贵,以至于成为基本应用程序设计中的决定性因素。
  • 在 JSP 中执行操作并不意味着不创建(甚至更少)对象。很可能恰恰相反。 JSP 被编译为完全正常的 Java 代码,该代码创建大量对象。
  • 听起来您的公司需要尽快雇用至少一名真正了解 Java 工作原理的人员。
  • Object creation in Java was somewhat expensive, about 15 years ago. Java runtimes have a improved a lot since then. Nowadays, Java object creation is often faster than in C++.
  • It was never so expensive that it would be a decisive factor in basic application design.
  • Doing things in a JSP does not mean no (or even fewer) objects are created. Quite the opposite, most likely. JSPs are compiled to perfectly normal Java code that creates lots of objects.
  • It sounds like your company needs to hire at least one person who actually understands how Java works ASAP.
静待花开 2024-10-26 09:19:09

对象创建很便宜。非常便宜。现代计算机速度很快。非常快。

做一些简单的分析。查看创建一百万个 POJO 需要多长时间。会很快的。

Object creation is cheap. Very cheap. And modern computers are fast. Very fast.

Do some simple profiling. See how long it takes to create a million of your POJOs. It'll be quite quick.

尘世孤行 2024-10-26 09:19:09

维护——大部分资金都花在维护软件而不是开发上。如果您使某些东西易于维护和扩展,那是一件幸事。

在这个社区中,进行任何过早的优化通常都会受到反对。您运行在什么样的硬件上?我认为服务器可以处理您的工作负载...我倾向于首先使其正常运行,然后找到瓶颈的想法。当您分析对象时,您可能会发现对象创建并不是最大的问题。

Maintenance - most dollars are spent maintaining software vs. developing. If you make something easy to maintain and extend, it's a blessing.

It's generally frowned upon in this community to do any premature optimization. What kind of hardware are you running on? I imagine servers can handle your workload... I tend to like the idea of making it work well first, and then finding bottle necks. You might find that object creation isn't the biggest problem when you profile it.

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