C# 计时函数精度测试

using System;
using System.Diagnostics;

namespace unittest
{

    class MainClass
    {
        public static void TestTimer1()
        {
            //
            //System.DateTime.Now.Ticks 与 System.Environment.TickCount一样误差很大
            //与stopwatch相比,误差约40-100MS !!
            //stopwatch与UnityEngine.Time.realtimeSinceStartup很接近,小于10MS

            //测试环境如下:
            //Unity2019 for mac
            //Visual Studio 2019 for mac

            //型号标识符:    MacBookPro16,1
            //处理器名称:    6 - Core Intel Core i7
            //处理器速度:    2.6 GHz
            //处理器数目:    1
            //核总数:    6
            //L2缓存(每个核):    256 KB
            //L3缓存:    12 MB
            //超线程技术:    已启用
            //内存:    16 GB
            //Boot ROM版本:    1037.100.362.0.0(iBridge: 17.16.14281.0.0, 0)
            //序列号(系统):    C02ZP9N6MD6M
            //硬件UUID:    ABB63E9E - F693 - 5620 - 8B44 - BF0A93F6897A
            //激活锁状态:    已停用

            var t1 = System.Environment.TickCount;//预热一下

            SomeWork();

            var t2 = System.Environment.TickCount;
            var ms = t2 - t1;

            Console.WriteLine("time1: " + ms);
        }

        public static void TestTimer2()
        {
            var wat = Stopwatch.StartNew();
            var t1 = wat.ElapsedMilliseconds;

            SomeWork();

            var t2 = wat.ElapsedMilliseconds;
            var dt = t2 - t1;
            Console.WriteLine("time2: " + dt);
        }

        public static void SomeWork()
        {
            //Console.WriteLine("Hello World!");

            int[] arr = new int[10];
            for (int i = 0; i < 10; i++)
            {
                arr[i] = i;
            }

            var sum = 0.0d;
            for (int i = 0; i < 100000000; i++)
            {
                sum += Math.Sqrt(i);
            }

            //arr[9] = arr[9];

            //for (int i = 0; i < 10; i++)
            //{
            //    Console.Write(arr[i] + ",");
            //}

            //Console.WriteLine("addfsfasfasfsdfasdfsdf");
        }
        public static void Main(string[] args)
        {
            TestTimer1();
            TestTimer2();
        }
    }
}

 

C# 计时函数精度测试

上一篇:linux系统上查看cpu信息相关命令


下一篇:Windows AD +DNS 配置