How to optimize this code to make it faster

Related searches

I am a really really new noobie high school student in java and we're currently optimizing a project but I'm stuck now. I think I've down everything I can do. Below are my 3 classes. 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. It's running between 1.49 and 1.38 currently.

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:

import 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:

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.

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); 
        } 

Make Java fast: Optimize!, Make Java fast: Optimize! How to get the greatest performance out of your code through low-level optimizations in Java -- complete with a� Also, this helps in refactoring the code to make it faster. We will get to it later. 2. Get the code working first. People have their own coding styles. Use the coding style that you are most comfortable with. For the first iteration, make the code work, at least and make the submission. See if it passes for all the test cases.


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);

How to optimize this code to make it faster, You can opt for: Math.random() // it has reliable efficiency. you can refer following program: int max = 5; int min = 1; int range = max - min + 1;� After discovering this faster alternative to mb_substr, I systematically removed every mb_substr and mb_strlen from the code I was working on. Lesson 5: Optimize for the most common case. Around 50% of the remaining runtime was spent in a method which expanded templates.


Code faster: 53 tips from the pros, The mechanical stuff is pretty easy to optimize; the options are limited. But the learning stuff has no limits. No one will ever know it all. You must make strategic� Make bugs impossible by design. Fail fast, use exceptions instead of null checks, use the type system to prevent data errors, and use automated testing. If you’re at a loss on where to begin, start with the part that you understand best. Write the code that would actually make a product first, no matter how silly or small that product is.


10 Tips for C and C++ Performance Improvement Code Optimization, Try to avoid implementing cheap tricks to make your code run faster. 1. Optimize your Code using Appropriate Algorithm. For any code you write,� There are times when you create some code that is particularly sensitive to performance changes. Perhaps it’s a piece of code that gets used in a lot of time-sensitive areas of your application, and you want it to be fast, and you want to make sure it stays fast for the life of the project. This is where unit testing is your friend.


How to Make Your Code Run Faster, I don't need to tell you why it's nice to have your code run fast. Here are some tricks I've found helpful, starting with the most bang for the buck. 1. Profile. You can't� Our Suggestion: This change will bring minor difference in time due to faster processors today. Moreover, you cannot have more than 255 characters in a line and you won't be able to debug your code using F8 efficiently. So it's a kind of useless, there is no reason to trade-off with readability with such a minor change in time.


In this article, some interesting optimization tips for Faster Python Code are discussed. These techniques help to produce result faster in a python code. Use builtin functions and libraries: Builtin functions like map() are implemented in C code. So the interpreter doesn’t have to execute the loop, this gives a considerable speedup.