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.

Community
  • 1
  • 1
Undefined
  • 655
  • 1
  • 4
  • 13
  • 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... – awksp May 23 '14 at 02:58
  • Oh wait, yes there would be a difference -- for the `Scanner`, not the `BracketMatcher`. – awksp May 23 '14 at 02: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. – Alvin Thompson May 23 '14 at 03: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. – Undefined May 23 '14 at 03:07

3 Answers3

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
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.

saurzcode
  • 827
  • 12
  • 30
Brad Peabody
  • 10,917
  • 9
  • 44
  • 63
  • 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. – Undefined May 23 '14 at 03:08
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

deme72
  • 1,113
  • 1
  • 10
  • 13