从类文件访问属性文件或静态常量更快吗?
我在 tomcat 容器中开发一个 Web 服务应用程序,该 Web 应用程序有很多属性,例如常量、错误消息等。
有什么更好更快的方法呢?
I develop a webservice application in a tomcat container, I have a lot of properties for the webapp like constants, error messages and so on.
What is the better and faster way to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一如既往,答案是分析它。
一般来说,这种微观优化几乎肯定无关紧要。不过,请随意分析一下它并看看。
The answer, as always, is profile it.
In general, this is the sort of micro-optimization that almost certainly won't matter. Feel free to profile it and see, though.
一般为静态常量; IO 活动总是会变慢。
但是,硬编码值意味着如果必须更改该值,则需要重新编译应用程序。作为开发人员,您可以决定是否需要在软件发布之外更改这些值。
Generally static constants; IO activity will always be slower.
However, hard-coding values means that the app will need to be recompiled if the value has to change. It's your call as a developer as to whether the values will ever need to be changed outside of a software release.
作为Bozho 说,就原始速度而言,你将很难击败
公共静态决赛
。但速度并不是一切。例如,如果您需要担心本地化问题,那么属性文件可能会更好,尽管您可能想查看ResourceBundle
代替。As Bozho says, in terms of raw speed, you're going to have trouble beating a
public static final
. But speed isn't everything. If you need to worry about localization at all, for instance, a properties file would probably be better, although you might want to look at aResourceBundle
instead.public static final String SOME_CONSTANT = "contstantValue";
这使得编译器内联该值,因此在运行时不会发生任何事情。
您还可以将值存储在任何位置(.properties 文件)并将它们加载到静态初始化块中。
public static final String SOME_CONSTANT = "contstantValue";
This makes the compiler inline the value, so you have nothing happening at runtime.
You can also store the values anywhere (.properties file) and load them in a static initializer block.