I've created a library for fun called GamblersDice. I'm trying to micro-optimize it but unsure if I'm doing it right. What I want is to use a reference to a global Random object when creating the die. The reason I believe it's not working is that testing GamblersDie(random, int) and GamblersDie(ref random, int) take nearly the same time over 10,000,000 iterations (test project is in the repo). If you don't want to scan through the repo, here are my constructors:
public class GamblersDie : Die
{
private Random _rnd;
public int[] Weight { get; private set; }
public GamblersDie() : this(new Random()) { }
public GamblersDie(int size) : this(new Random(), size) { }
public GamblersDie(params int[] weights) : this(new Random(), weights) { }
public GamblersDie(Random rnd) : this(ref rnd) { }
public GamblersDie(Random rnd, int size) : this(ref rnd, size) { }
public GamblersDie(Random rnd, params int[] weights) : this(ref rnd, weights) { }
public GamblersDie(ref Random rnd) : this(ref rnd, 6) { }
public GamblersDie(ref Random rnd, int size) {
_rnd = rnd;
Weight = new int[size];
for (int i = 0; i < Weight.Length; i++)
{
Weight[i] = 1;
}
}
public GamblersDie(ref Random rnd, params int[] weights) : this(ref rnd, weights.Length)
{
for (int i = 0; i < Weight.Length; i++)
{
Weight[i] = weights[i];
}
}
}
Like I said, this is just for fun. I want to micro-optimize it, just because it may be possible. Another question I have is about the constructor chaining. At first glance it may be confusing and I'm wonder if it's some kind of anti-pattern.