【问题标题】:How to measure time for maze generation?如何测量迷宫生成的时间?
【发布时间】:2014-02-05 14:39:14
【问题描述】:

如何在我的代码中插入计时器?我的目标是知道我的迷宫需要多长时间才能生成,因为我将它与我的论文之前的程序进行了比较。

感谢您的大力帮助。 :)

这是我使用的代码:

public class Maze extends JPanel {

private Room[][] rooms;// m x n matrix of rooms
private ArrayList<Wall> walls; // List of walls
private Random rand;// for random wall
private int height;// height of matrix
private int width;// width of matrix
private int num;// incrementor
private JoinRoom ds;// union paths

// paint methods //
private int x_cord; // x-axis rep
private int y_cord;// y-axis rep
private int roomSize;
private int randomWall;

public Maze(int height, int width) {
    this.height = height;
    this.width = width;
    rooms = new Room[height][width];
    walls = new ArrayList<Wall>((height - 1) * (width - 1));
    generateRandomMaze();
    setPreferredSize(new Dimension(800, 700));
 }
private void generateRandomMaze() {
    generateInitialRooms();// see next method
    ds = new JoinRoom(width * height);
    rand = new Random(); // here is the random room generator
    num = width * height;

    while (num > 1) {
       // when we pick a random wall we want to avoid the borders getting eliminated
        randomWall = rand.nextInt(walls.size());
        Wall temp = walls.get(randomWall);
        // we will pick two rooms randomly 
        int roomA = temp.currentRoom.y + temp.currentRoom.x * width;
        int roomB = temp.nextRoom.y + temp.nextRoom.x * width;

        // check roomA and roomB to see if they are already members 
        if (ds.find(roomA) != ds.find(roomB)) {
            walls.remove(randomWall);
            ds.unionRooms(ds.find(roomA), ds.find(roomB));
            temp.isGone = true;
            temp.currentRoom.adj.add(temp.nextRoom);
            temp.nextRoom.adj.add(temp.currentRoom);
            num--;
        }// end of if
    }// end of while
}

 // name the room to display
private int roomNumber = 0;
/**
 * Sets the grid of rooms to be initially boxes
 * This is self explanitory, we are only creating an reverse L for all
 * The rooms and there is an L for the border
 */
private void generateInitialRooms() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            // create north walls
            rooms[i][j] = new Room(i, j);
            if (i == 0) {
                rooms[i][j].north = new Wall(rooms[i][j]);
            } else {
                rooms[i][j].north = new Wall(rooms[i - 1][j], rooms[i][j]);
                walls.add(rooms[i][j].north);
            }
            if (i == height - 1) {
                rooms[i][j].south = new Wall(rooms[i][j]);
            }
            if (j == 0) {
                rooms[i][j].west = new Wall(rooms[i][j]);
            } else {
                rooms[i][j].west = new Wall(rooms[i][j - 1], rooms[i][j]);
                walls.add(rooms[i][j].west);
            }
            if (j == width - 1) {
                rooms[i][j].east = new Wall(rooms[i][j]);
            }
            rooms[i][j].roomName = roomNumber++;// we will name the rooms
        }
    }
    // initalize entrance and exit
    rooms[0][0].west.isGone = true;// you can replace .west.isGone with .north.isGone
    // this is just saying the roomName for top left is 0 
    rooms[0][0].roomName = 0;
    // we will remove the south wall of the last room
    rooms[height - 1][width - 1].south.isGone = true;
    // this is just saying the roomName for bottom right is the last element in the mxn room matrix
    rooms[height - 1][width - 1].roomName = (height * width);
}

 public void paintComponent(Graphics g) {
    x_cord = 40;
    y_cord = 40;
    // could have taken height as well as width
    // just need something to base the roomsize
    roomSize = (width - x_cord) / width + 7;

    // temp variables used for painting
    int x = x_cord;
    int y = y_cord;

    for (int i = 0; i <= height - 1; i++) {
        for (int j = 0; j <= width - 1; j++) {
            if (!(rooms[i][j].north.isGone)) {
                g.drawLine(x, y, x + roomSize, y);
            }//end of north if
            // west wall not there draw the line
            if (rooms[i][j].west.isGone == false) {
                g.drawLine(x, y, x, y + roomSize);
            }// end of west if
            if ((i == height - 1) && rooms[i][j].south.isGone == false) {
                g.drawLine(x, y + roomSize, x + roomSize,
                        y + roomSize);
            }// end of south if
            if ((j == width - 1) && rooms[i][j].east.isGone == false) {
                g.drawLine(x + roomSize, y, x + roomSize,
                        y + roomSize);
            }// end of east if
            x += roomSize;// change the horizontal
        }// end of inner for loop
        x = x_cord;
        y += roomSize;
    }// end of outer for loop
}

  public static void main(String[] args) {
    // we will use the scanner for userInput
    Scanner userInput = new Scanner(System.in);
    int m, n;// these are variables for the size of maze (m x n)
    System.out.print("Enter the size of your maze: ");
    // store the input
    m = userInput.nextInt();
    n = userInput.nextInt(); 

    // use JFrame to put the created panel on 
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 800);
    frame.getContentPane().add(new Maze(m, n));
    frame.pack();
    frame.setVisible(true);
}// end of main
  }// END OF CLASS 

【问题讨论】:

  • System.currentTimeMillis()
  • 在任何 Java 文档中查找时间函数应该是相当简单的,但您还没有说明您是否尝试过此操作以及遇到了什么问题。如果你能写这段代码,你肯定可以搜索时间函数吗?

标签: java eclipse swing


【解决方案1】:

我曾经写了一个 Timer 类来做这个,你可以在这里找到它:https://github.com/twothe/newdawn/blob/master/two/newdawn/util/TimeCounter.java

对于大多数任务来说,它非常简单且足够。

Java 的一般困难在于代码不是在固定时间执行的。介于垃圾收集器之间的某个地方可能会中断您的计时,或者 JIT 认为一段代码变慢并突然对其进行优化。所有这些事情都会弄乱使用System.nanoTime() 所做的任何测量,因此不要将数字视为事实,而应将其视为趋势。

如果您想获得准确的数字,则需要使用更复杂的工具,尤其是运行有问题的代码一千或十亿次以排除背景噪音,但即便如此,这些数字也仅对您的本地计算机有效,并且在不同的硬件上可能完全不同。

【讨论】:

    【解决方案2】:

    这取决于您想要的信息。最简单的方法是在开始生成之前使用 System.currentTimeInMillis(),然后再使用,然后比较结果。这将为您提供生成所需的毫秒数。

    例如

    walls = new ArrayList<Wall>((height - 1) * (width - 1));
    long startTime = System.currentTimeMillis();    
    generateRandomMaze();
    long endTime = System.currentTimeMillis();    
    System.out.println("Time Taken: " + (endTime-startTime) + "ms");
    setPreferredSize(new Dimension(800, 700));
    

    【讨论】:

    • 有几个错误:1)没有方法currentTimeInMillis。 2) 字符串不能在数学上添加长数字。
    • 1) 当我谈论它时,我说当前时间以毫秒为单位,并且输入相同,哎呀! 2)我事后添加了字符串,但错过了括号。感谢您的提醒。
    • 附注2不是几个:p
    • 我想,我会再找一个,但我没有。最后我忘记将 several 更改为 two :p 但总的来说,有 3 个错误,对吧? ;)
    • 这不是错误,这只是意味着他的代码很快。其他错误是编码错误。我确实说过这取决于他如何想要这些信息。如果他想让它达到纳秒精度,他可以改变它。我为他的问题提供了一个解决方案,这很可能会做他需要做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多