Question

I wrote a Sudoku puzzle solver using brute force recursion. Now, I wanted to see how long it would take to solve 10 puzzles of similar types. So, I made a folder called easy and placed 10 "easy" puzzles in the folder. When I run the solver the first time it may take 171 ms, the second time it takes 37 ms, and the third run takes 16 ms. Why the different time for solving the exact same problems over again? Shouldn't the time be consistent?

The second problem is that is only displaying the last puzzle solved even though I tell it to repaint the screen after loading the puzzle and again after solving it. If I load just a single puzzle without solving it it will show the initial puzzle state. If I then call the Solve method the final solution is drawn on screen. Here is my method that solves multiple puzzles.

void LoadFolderAndSolve() throws FileNotFoundException {

    String folderName = JOptionPane.showInputDialog("Enter folder name");
    long startTime = System.currentTimeMillis();

    for (int i = 1; i < 11; i++) {
        String fileName = folderName + "/puzzle" + i + ".txt";
        ReadPuzzle(filename);  // this has a call to repaint to show the initial puzzle
        SolvePuzzle();         // this has a call to repaint to show the solution
        // If I put something to delay here, puzzle 1-9 is still not shown only 10.
    }

    long finishTime = System.currentTimeMillis();
    long difference = finishTime - startTime;

    System.out.println("Time in ms - " + difference);
}
Was it helpful?

Solution

The first time it runs the JVM needs to load the classes, create the objects you're using etc - it takes more time. Further, it always takes time for the JVM to "start kicking", which is why, when profiling, usually running a few thousands of loops and dividing the result to get a better estimation.

For the second problem it's impossible to help you without seeing the code, but a good guess would be that you're not "flushing" the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top