Is this method faster than Math.random ()?

I'm a newbie and am currently starting work on a game for Android that uses a particle swarm optimization algorithm. I'm trying to optimize my code a bit, and I have quite a lot of Math.random () for for-loops, which works almost all the time. So I was thinking of a way to get around and skip all calls to Math.random ().

Using this method:

float random[] = new float[100]; static int randomIndex=0; private float myRandom(){ if(randomIndex >= 99) randomIndex = 0; else randomIndex = randomIndex+1; return random[randomIndex]; } 

... and also do this one action start time:

 for (int i=0; i< 100; i++) random[i]=(float) Math.random(); 

My question is, would it be better (faster) than using Math.random ()? Does anyone have a better suggestion on how to do this?

I also wonder if anyone knows a good site where I can learn more about how to write efficient java / android code. I am afraid that I suck it.

+4
source share
3 answers

Learn to trust the Java API: it is powerful and fast. "Premature optimization is the root of all evil." Do your work on the program before you start worrying about such things.

For your purpose, however, you can easily figure out how to compare your method with Java in a loop of, say, 30 million, and then compare the time.

Not to mention filling an array of 100 random numbers, then its use a million times is not random at all.

+7
source

It will certainly be faster, but it will not be β€œbetter” in the sense that it will not generate very good random numbers. Perhaps this is normal for your game; perhaps this is not so.

There are several options for inserting java.util.Random , which can be fast. You can check out the Uncommons Maths library, but there are others too. Often there is "XORShiftRNG", which is designed for quick work.

Be careful not to make premature optimization: if Math.random is fast enough in practice, then do not bother to find a replacement. Run this solution only after some analysis of the actual time spent in this section of code.

+3
source

Here is an article about optimization http://www.javaworld.com/javaworld/jw-04-1997/jw-04-optimize.html?page=1

What you are talking about is a lookup table for your random numbers. In most cases, this happens faster, but in this case it will also change the results.

In general, my approach always finds the one that takes the most time, and research it as deep as I can. Make sure you're not doing too much work, and look at things that seem strangely slow. Optimized code usually spends most of the time on basic math operations. Try to keep going until it looks like you spend most of your time only in mathematical operations.

Your code is doing something, and there may be a better way to do this. So keep your ear on the ground for a better algorithm.

And keep in mind, there are all kinds of Android devices. It will be difficult to optimize for all of them. Sometimes you have to make a solid run during optimization, and then assume that it’s as good as it is going to get and start to reduce the amount of work performed or decide that it is simply impossible.

+1
source

Source: https://habr.com/ru/post/1343557/


All Articles