开发者

Java performance issue

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 clas开发者_JS百科s 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.


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.


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. :-)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜