Strawberry
Strawberry

Reputation: 67888

How can I populate a 100,000,000 element array?

I was thinking of storing a random number in each element using a loop. Is there a better way to do this? I need random numbers to test my sorts and the timing.

Upvotes: 3

Views: 1243

Answers (6)

Steven Schlansker
Steven Schlansker

Reputation: 38526

I've not bothered to test whether this is an actual speedup, but you could consider making a ByteBuffer of the correct size (4 bytes * 100000000 elements), and then fill it with Random.nextBytes. From there, call asIntBuffer on it and you will get a buffer full of integers.

I have proved neither that this is faster nor that it does not affect the distribution of your results - but if you are actually looking for speed, you could try it out.

Upvotes: 0

Bill99
Bill99

Reputation: 395

Why do you need to store the values? If you set the seed value, you will get the same sequence of values from random.

Random r = Random(856007); // or any number to identify a given sequence

n = r.nextInt();

Upvotes: 0

Moyshe
Moyshe

Reputation: 1122

You can always go to multithreading. The size of the array should warrant the cost of creating threads.

import java.util.Random;

public class SimpleTest implements Runnable {

    public static int[] array = new int[1000];
    private int startPoint;

    public SimpleTest(int startPoint) {
        this.startPoint = startPoint;
    }   

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        Thread test1 = new Thread(new SimpleTest(0));
        Thread test2 = new Thread(new SimpleTest(1));
        test1.start(); test2.start();
        while(test1.isAlive() || test2.isAlive()) {
            Thread.sleep(10);
            System.out.println("Waiting...");
        }
        System.out.println("Done!");
    }

    public void run() {
        Random r = new Random();
        for (int i = startPoint; i < array.length; i+=2) {
            array[i] = r.nextInt();
            System.out.println("Thread "+(startPoint+1)+" has put a value no. "+i+" and it was: "+array[i]);
        }
    }   

}

EDIT Hmm, i checked execution times and there's no improvement on my double cored machine. It seems that the the size of an array does not warrant the cost of creating threads, or i'm missing something else here.

EDIT2 When i split it on 8 threads (;P) the improvement was quite big - i went down form starting ~3900ms to ~2500. There's something onto this...

Upvotes: 0

Stephen C
Stephen C

Reputation: 718906

The simple approach is going to be the best:

  • Allocate the large array
  • For each element
    • Generate random number
    • Assign to element.

The bottleneck will be the calls to the random number generator, and you cannot avoid those ... if you want random numbers.

Just write the code in a simple fashion and let the JIT compiler worry about optimizing it. Only get involved if profiling your application reveals that this part of your application is a significant performance issue.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

A simple pseudocode:

int[] random = new int[10000000];

for (int i = 0; i < random.length; i++) {
   random[i] = generateAbsoluteRandom();
}

Upvotes: 5

Steve J
Steve J

Reputation: 2674

Does it need to be a different random set each time? If not, generate one random array of the size you need, serialize it, and store it. The load it in when you need it. That might run faster.

Upvotes: 0

Related Questions