0

How do I make this code more effective (faster)?

It's about a game that's called "Pebble Solitaire". It's the same as Peg Solitaire but with only one line (pebbles not placed in a cross).

If it is "oooooooooo-ooooooooooo-" the output should be the least amount of pebbles left out of every scenario of combination of moves.

How can this become more effective?

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        long startTime = System.currentTimeMillis();

        posSol = new ArrayList<>();
        posSol.add(reader.nextLine());

        int minimumNoOfPebbles = 23;


        for (int y = 0; y < posSol.size(); y++) {
            String s = posSol.get(y);

            for (int i = 0; i <= s.length() - 1; i++) {
                if (s.charAt(i) == 'o') {
                    if (i <= s.length() - 2 && s.charAt(i + 1) == 'o' && s.charAt(i + 2) == '-') {
                        String temp = s.substring(0, i) + "--o" + s.substring(i + 3);
                        posSol.add(temp);
                    }
                    if (i >= 2 && s.charAt(i - 1) == 'o' && s.charAt(i - 2) == '-') {
                        String temp = s.substring(0, i - 2) + "o--" + s.substring(i + 1);
                        posSol.add(temp);
                    }
                }
            }

            int counter = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'o') {
                    counter++;
                }
            }
            if (minimumNoOfPebbles > counter) {
                minimumNoOfPebbles = counter;
            }
        }

        System.out.println(minimumNoOfPebbles);

        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println(totalTime);

    }
A. Savva
  • 632
  • 10
  • 30
  • 1
    Is this code working? If not, where's the problem (other than speed)? If the only problem is speed, have you isolated the longest running process? – OneCricketeer Nov 20 '16 at 15:52
  • this code is working, although it takes around 30 seconds. It should take much less than a second. No I haven't done that, how do I do that? – A. Savva Nov 20 '16 at 15:58
  • Move around your timing print statements or set breakpoints. Or look at http://codereview.stackexchange.com/tour – OneCricketeer Nov 20 '16 at 16:01
  • quick guess: too much string concatenation in the loop. – Robert Nov 20 '16 at 16:47
  • well its not quite clear what is this method trying to accomplish, but from a fast look you are looping inside a list , and in the loop you are adding more elements in the list so you are causing many native calls to increase the list size , which is already binded in the for loop as the `posSol.size()` max. So as @cricket_007 mentioned, i am not quite sure if this code is working or at least if it does what you expect. Or else use an iterator , like [this answer](http://stackoverflow.com/questions/23674666/how-to-add-values-to-a-list-while-iterating-it) – AntJavaDev Nov 20 '16 at 16:51

0 Answers0