1.4.39

1.4.39 #

解答 #

执行 N 次后取平均即可。

代码 #

修改后的 DoublingTest:

public static class DoubleTest
{
    private static readonly int MaximumInteger = 1000000;

    /// <summary>
    /// 返回对 n 个随机整数的数组进行一次 ThreeSum 所需的时间。
    /// </summary>
    /// <param name="n">随机数组的长度。</param>
    /// <param name="repeatTimes">重复测试的次数。</param>
    /// <returns></returns>
    public static double TimeTrial(int n, int repeatTimes)
    {
        var a = new int[n];
        double sum = 0;
        var random = new Random(DateTime.Now.Millisecond);
        for (var i = 0; i < n; i++)
        {
            a[i] = random.Next(-MaximumInteger, MaximumInteger);
        }
        for (var i = 0; i < repeatTimes; i++)
        {
            var timer = new Stopwatch();
            ThreeSum.Count(a);
            sum += timer.ElapsedTime();
        }
        return sum / repeatTimes;
    }
}

另请参阅 #

Measurement 库