我創(chuàng)建了一個名為GamblersDice 的娛樂庫。我正在嘗試對其進行微優(yōu)化,但不確定我是否做得對。我想要的是在創(chuàng)建模具時使用對全局Random對象的引用。我認為它不起作用的原因是測試GamblersDie(random, int)和GamblersDie(ref random, int)花費幾乎相同的時間超過 10,000,000 次迭代(測試項目在 repo 中)。如果你不想瀏覽 repo,這里是我的構造函數(shù):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]; } }}就像我說的,這只是為了好玩。我想對它進行微優(yōu)化,只是因為它可能是可能的。我的另一個問題是關于構造函數(shù)鏈接。乍一看它可能令人困惑,我想知道它是否是某種反模式。
不確定按引用傳遞是否有效
慕無忌1623718
2021-10-24 19:50:25