zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    Linux开发初探
    Linux开发初探
    电脑使用说明书
    电脑使用说明书
    Delegates, Events, and Anonymous Methods 委托、事件与匿名方法
    CSS haslayout
    js与flash结合使用
    sgen.exe" exited with code 1.解决方法
    selenium
    selenium支付高版本的FireFox
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11211961.html
Copyright © 2011-2022 走看看