Fixed-size arrays and randomShuffle()

Vidar Wahlberg canidae at exent.net
Thu May 3 08:53:07 PDT 2012


On 2012-05-03 17:31, Chris Cain wrote:
> You might want to post your code...

Sure!
D:
---
import std.random;
import std.stdio;
void main() {
         auto iterations = 10000000;
         int[] a;
         for (int i = 0; i < 42; ++i)
                 a ~= i;
         for (int i = 0; i < iterations; ++i)
                 randomShuffle(a);
}

naushika:~/projects> dmd random.d && time ./random
./random  38,35s user 0,05s system 99% cpu 38,420 total
---


Java (7):
---
import java.util.ArrayList;
import java.util.Collections;
public class Rnd {
         public static void main(String... args) {
                 int iterations = 10000000;
                 ArrayList<Integer> a = new ArrayList<Integer>();
                 for (int i = 0; i < 42; ++i)
                         a.add(i);
                 for (int i = 0; i < iterations; ++i)
                         Collections.shuffle(a);
         }
}

naushika:~/projects> javac Rnd.java && time java Rnd
java Rnd  9,92s user 0,03s system 100% cpu 9,922 total
---


More information about the Digitalmars-d-learn mailing list