0

I've read some other questions about declaring objects inside loops, like:

Is it Better practice to Declare individual objects or loop Anonymous objects into ArrayList?

Java : declaring objects in a loop

but neither really address my question.

I'm scanning for user input repeatedly and creating a class to parse that string every iteration:

    public static void main(String[] args) {
        while (true) {
            System.out.print("Enter a string of brackets to test: ");
            String exp = new Scanner(System.in).nextLine().trim();
            if (exp.equals("q")) break; // q is the 'quit' flag
            System.out.println(new BracketMatcher(exp.length()).parse(exp));
        }
    }

Is there any difference - in performance, not scope - to this block?:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    BracketMatcher matcher;
    while (true) {
        System.out.print("Enter a string of brackets to test: ");
        String exp = input.nextLine().trim();
        if (exp.equals("q")) break; // q is the 'quit' flag
        matcher = new BracketMatcher(exp.length());
        System.out.println(matcher.parse(exp));
    }

Would I be better off making parse() a static method in BracketMatcher since I only use that method?

Thanks.

4
  • I'd imagine that if there were a difference, it'd quickly vanish once the JIT compiler go to it. I don't know for sure though... Commented May 23, 2014 at 2:58
  • Oh wait, yes there would be a difference -- for the Scanner, not the BracketMatcher. Commented May 23, 2014 at 2:59
  • 1
    Yes there is a difference in performance, but it doesn't matter. Since you're waiting for user input, that wait completely dwarfs any performance gains to be had. Any performance difference would be insignificant in comparison. Commented May 23, 2014 at 3:03
  • @AlvinThompson Since you're waiting for user input, that wait completely dwarfs any performance gains to be had is a great point. But I don't care if it matters - I'm curious. Commented May 23, 2014 at 3:07

3 Answers 3

2

The performance difference in your code comes from creating a new Scanner in every iteration (which is silly, maybe not even work reliably, depending how a Scanner buffers).

Where you declare the variable has no performance impact in itself.

Personally I would create the Scanner once (because it is supposed to read all lines, not just a single one), but the BracketMatcher inside of the loop (because it is tied to the current line).

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.print("Enter a string of brackets to test: ");
        String exp = input.nextLine().trim();
        if (exp.equals("q")) break; // q is the 'quit' flag
        System.out.println(new BracketMatcher(exp.length()).parse(exp));
    }
}

Would I be better off making parse() a static method in BracketMatcher since I only use that method?

I don't know what your BracketMatcher does, and if anything can be prepared regardless of input. For example regular expression matchers can be compiled once for a fixed expression and then re-used for many strings to match. In that case, you may keep the BracketMatcher a stateful object, and create it once outside the loop.

Sign up to request clarification or add additional context in comments.

Comments

0

If you create the scanner multiple times you lose performance(because of garbage collection and constructing it) and you gain a slightly buggier system because of spammed input scanners

Comments

0

Doing an allocation new Scanner(...) is generally going to be less performer than re-using the object. Especially if there is no internal state involved in the object itself - then allocating a new one each time does nothing for you.

So yes, I'd say make one of each and re-use them.

Would I be better off making parse() a static method in BracketMatcher since I only use that method?

If BracketMatcher contains no state then yes you could make it a static method.

1 Comment

I'm creating a stack of size str.length() in the BracketMatcher constructor. If I made the method static I would be creating a new stack each time parse() is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.