Java性能问题
我有一个与 java 性能和方法执行相关的问题。
在我的应用程序中有很多地方必须验证某些参数,因此我编写了一个 Validator 类并将所有验证方法放入其中。这是一个例子:
public class NumberValidator {
public static short shortValidator(String s) throws ValidationException{
try{
short sh = Short.parseShort(s);
if(sh < 1){
throw new ValidationException();
}
return sh;
}catch (Exception e) {
throw new ValidationException("The parameter is wrong!");
}
}
...
但我正在考虑这一点。这样可以吗?它是面向对象且模块化的,但是考虑到性能,这是一个好主意吗? 如果我同时进行大量调用怎么办?上面的代码片段简短而快速,但有些方法需要更多时间。
当大量调用同一个类中的静态方法或实例方法并且该方法不同步时会发生什么情况?所有的调用方法都必须排成一行,并且 JVM 按顺序执行它们?
拥有一些与上述相同的类并随机调用它们相同的方法是个好主意吗?我认为不是,因为“不要重复自己”和“重复是邪恶的”等。但是性能呢?
感谢是提前的。
I've got a question related to java performance and method execution.
In my app there are a lot of place where I have to validate some parameter, so I've written a Validator class and put all the validation methods into it. Here is an example:
public class NumberValidator {
public static short shortValidator(String s) throws ValidationException{
try{
short sh = Short.parseShort(s);
if(sh < 1){
throw new ValidationException();
}
return sh;
}catch (Exception e) {
throw new ValidationException("The parameter is wrong!");
}
}
...
But I'm thinking about that. Is this OK? It's OO and modularized, but - considering performance - is it a good idea?
What if I had awful lot of invocation at the same time? The snippet above is short and fast, but there are some methods that take more time.
What happens when there are a lot of calling to a static method or an instance method in the same class and the method is not synchronized? All the calling methods have to fall in line and the JVM executes them sequentially?
Is it a good idea to have some class that are identical to the above-mentioned and randomly call their identical methods? I think it is not, because "Don't repeat yourself " and "Duplication is Evil" etc. But what about performance?
Thanks is advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于方法的可重入性:如果它是静态的,则它不保持任何状态,因此它是完全安全的。
关于性能:看看您的用例。由于您正在验证字符串,我只能假设您正在验证用户输入。无论如何,系统的并发用户数量不太可能产生性能瓶颈。
On reentrancy of your method: if it's static, it doesn't hold any state, so it's perfectly safe.
On performance: look at your use cases. Since you're validating Strings, I can only assume you are validating user input. In any case, the number of simultaneous users of your system is not probable to incur a performance bottleneck.
仅两条评论:
1)将验证分解为方法实际上可能会稍微提高性能。据我所知,JIT编译器旨在检测频繁的方法调用。因此,验证方法是 JIT 优化的良好候选方法。
2) 尝试避免“catch(Exception e)”。不建议这样做,因为您还要捕获各种 RuntimeException。如果您在其中一项重要验证中存在错误,则可能会抛出隐藏代码中错误的 ValidationException。
Just two comments:
1) Factoring out validation into methods may in fact improve performance a little. As far as I know, the JIT compiler is designed to detect frequent method invocations. The validation methods are therefore good candidates for JIT optimization.
2) Try avoiding 'catch(Exception e)'. This is not recommended since you are capturing all kinds of RuntimeException as well. If you have a bug in one of the non-trivial validations, you may throw a ValidationException that hides a bug in the code.
不确定您关心什么。
由于您提到该方法未同步,我想您有来自多个线程的并发调用。
而且由于该方法不是同步的,因此任何调用都可以毫无问题地并发执行。
当然,通过复制并粘贴此方法到调用类中,您不会获得任何性能改进。也许你会降低性能,因为你的代码大小会增加,并且会浪费处理器缓存中的空间,但对于这么短的方法,我认为这是一个可分析的效果。
Not sure what are your concern.
Since you mentioned the method not beeing synchronized I suppose you have concurrent invocations from multiple threads.
And since the method is not sychronized any invocation can be executed concurrently without problems.
For sure you won't get any performance improvements by copy and paste this method in the calling classes. May be you would reduce performance because your code size will increase and will waste space in che processor cache, but for such a short method I think it's a trascurable effect.
您遇到性能问题吗?
首先让代码易于维护,如果达不到预期,它也会易于阅读和重构。
如果您对每个选择的速度都进行了优化,那么您通常会遇到一些不可读的情况,并且必须从头开始修复简单的错误。
也就是说,您的方法是静态,并且只需要初始化一次。这是快速版本。 :-)
Are you experiencing performance problems?
Make the code easy to maintain first, and if it's not living up to expectations, it'll be easy to read and easy to refactor.
If you make the code optimized for speed on every choice, you'll often wind up with something unreadable, and have to start from scratch to fix simple bugs.
That said, your method is a static, and will only need to be initialized once. That is the fast version. :-)
我对这段代码表示怀疑,与其说是出于性能原因,不如说我认为它没有成功地抽象出任何值得抽象的东西。
如果是为了检查用户输入,它会用“ValidationException”替换合理的错误消息,例如“允许的小部件的最大数量为 9999”。如果您添加诸如参数(或 try/catch 子句)之类的内容来在上下文中获取正确的消息,那么几乎可以肯定,所需的调用站点代码比直接的处理方式更复杂,更难以编写和维护。
如果是为了内部健全性检查,如果您将参数作为字符串到处传递并不断解析和验证它们,那么您很可能会开始失去有意义的性能,并且肯定会大大增加复杂性和错误。
I'm suspicious of this code, not so much on performance grounds as that I don't think it is successfully abstracting out anything that deserves abstraction.
If it is for checking user input, it replaces reasonable error messages like 'maximum number of widgets allowed is 9999' with 'ValidationException'. And if you add something like arguments (or try/catch clauses) to get the messages right in context, then almost certainly the required call-site code is more complex, harder to write and maintain, than the straightforward way of doing things.
If it is for internal sanity checking, you may well start to lose meaningful performance, and certainly greatly increase complexity and bugginess, if you are passing arguments round as strings all over the place and continually parsing and validating them.