• C#-----计算程序运行时间


    1.DateTime类

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                long sum = 0;
                DateTime beforDT = System.DateTime.Now;
                for (int i = 0; i < 10000000; i++)
                {
                    sum += i;
                }
                DateTime afterDT = System.DateTime.Now;
                TimeSpan ts = afterDT.Subtract(beforDT);
                Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);
                Console.ReadLine();
            }
        }
    }

    2.Stopwatch类

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                long sum = 0;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    sum += i;
                }
                sw.Stop();
                TimeSpan timeSpan = sw.Elapsed;
                Console.WriteLine("Stopwatch总共花费{0}ms.", timeSpan.TotalMilliseconds);
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    modf()函数
    面向对象编程五大原则
    .Net网络资源
    整理CentOS常用命令
    在RHEL5上安装oracle10gLinux
    strchr()函数
    swab函数
    Strstr()函数
    tmpnam函数
    strdup ()函数
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11211961.html
Copyright © 2020-2023  润新知