简体   繁体   English

如何优化此代码以使其更快

[英]How to optimize this code to make it faster

I am a really really new noobie high school student in java and we're currently optimizing a project but I'm stuck now.我是一个非常新的 Java 新手高中生,我们目前正在优化一个项目,但我现在被卡住了。 I think I've down everything I can do.我想我已经竭尽全力了。 Below are my 3 classes.下面是我的 3 个班级。 PS, These codes print out the time spent in while loop and I'm trying to get it down to less than 1 sec on my computer. PS,这些代码打印出在 while 循环中花费的时间,我试图在我的计算机上将其缩短到 1 秒以内。 It's running between 1.49 and 1.38 currently.它目前在 1.49 和 1.38 之间运行。

main:主要的:

public class code {
    public static void main(String[] args) {
        int numRows = 30;
        int numCols = 30;
        int start = 31;
        int exit = 868;
        int numKittens = 30_000;
        KittenBox box = new KittenBox(numRows, numCols, start, exit, 
                  numKittens);
        double a = 10;
        box.play();
    }
}

kitten.java:小猫.java:

import java.util.SplittableRandom;导入 java.util.SplittableRandom;

public class Kitten {

private int rows;
private int columns;
public int square;
private SplittableRandom a;

public Kitten(int rows, int columns, int square) {
    this.rows = rows;
    this.columns = columns;
    this.square = square;
    a = new SplittableRandom();
}

public int move() {
    int i = a.nextInt(1, 5);
    return (i == 1 && (!(this.square < columns))) ? 
            this.square -= rows : ((i == 2 && (!(this.square >= columns * 
            (rows - 1)))) ? this.square += rows : ((i == 3 && (!(this.square
            % rows == 0))) ? this.square -= 1 : ((!(this.square % rows == 
            rows - 1)) ? this.square += 1 : this.square)));
}

} }

kittenbox.java: kittenbox.java:

public class KittenBox {

    private ArrayList<Kitten> kitten;
    private int numRows;
    private int numCols;
    private int start;
    private int exit;
    private int numKittens;

    public KittenBox(int numRows, int numCols, int start, int exit, int numKittens) {
        this.numRows = numRows;
        this.numCols = numCols;
        this.start = start;
        this.exit = exit;
        this.numKittens = numKittens;
        kitten = new ArrayList<>();

    }
    public void play() {
        for (int i = 0; i < numKittens; i++) {
            kitten.add(new Kitten(numRows, numCols, start));
        }
        long startTime = System.nanoTime();
        while (!kitten.isEmpty()) {
            for (int i = 0; i < kitten.size(); i++) {
                kitten.get(i).move();
                if (kitten.get(i).square == exit) {
                    kitten.remove(i);
                }
            }
        }
        long endTime = System.nanoTime();
        System.out.format("Kittens took %f seconds to escape.\n",
                (endTime - startTime) / 1000000000.0);
    }
}

but I still can't speed my code up to the benchmark.但我仍然无法将我的代码加速到基准测试。 is there any way that's faster?有没有更快的方法?

thanks a lot.多谢。

You can opt for:您可以选择:

Math.random() // it has reliable efficiency . Math.random() // 它具有可靠的效率

you can refer following program:您可以参考以下程序:

        int max = 5; 
        int min = 1; 
        int range = max - min + 1; 

        // generate random numbers within 1 to 10 
        for (int i = 0; i < max; i++) { 
            int rand = (int)(Math.random() * range) + min; 

            // Output is different everytime this code is executed 
            System.out.println(rand); 
        } 

Not sure how fast this is, but you could try this不确定这有多快,但你可以试试这个

int randomNumber = (int)(Math.random() * range_size + starting_num);
int one_to_four = (int)(Math.random() * 4 + 1);

Your problem is most likely that this terrible mess of a function you call move() does not quite work the way you think it works, and so it is causing kittens to reach 'exit' a lot less frequently than you think they would.你的问题很可能是你调用的这个糟糕的函数move()并没有像你认为的那样工作,所以它导致小猫到达“退出”的频率比你想象的要低得多。

The way this question is worded, this should be enough to help you get on your way.这个问题的措辞方式应该足以帮助您继续前进。 (Besides, it appears to be homework, and for homework we tend to point to solutions without providing actual solutions.) (此外,它似乎是家庭作业,对于家庭作业,我们倾向于指出解决方案而不提供实际解决方案。)

Before anyone can help you any more with this, you are going to have to rewrite that move() method to make it understandable by human beings.在任何人都可以帮助您之前,您将不得不重写该move()方法以使其能够被人类理解。

  • Hint 1: use a switch() statement.提示 1:使用switch()语句。

  • Hint 2: add a default: cause to your switch statement, which, if reached, throws an exception.提示 2:在您的switch语句中添加一个default: cause,如果到达,将引发异常。 This way you will know you are not generating numbers that are never handled.这样你就会知道你没有生成从未处理过的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM