您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在C#中对小型代码样本进行基准测试,是否可以改善此实现?

在C#中对小型代码样本进行基准测试,是否可以改善此实现?

这是修改后的功能:根据社区的建议,随时对其进行修订,使其成为社区Wiki。

static double Profile(string description, int iterations, Action func) {
    //Run at highest priority to minimize fluctuations caused by other processes/threads
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
    Thread.CurrentThread.Priority = ThreadPriority.Highest;

    // warm up 
    func();

    var watch = new Stopwatch();

    // clean up
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();

    watch.Start();
    for (int i = 0; i < iterations; i++) {
        func();
    }
    watch.Stop();
    Console.Write(description);
    Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);
    return watch.Elapsed.TotalMilliseconds;
}

确保 。最后一部分很重要,因为即使在发布模式下,JIT也会通过附加的调试器来限制其优化。

c# 2022/1/1 18:14:14 有613人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶