您当前的位置: 首页 >  令狐掌门 c#

C# Stopwatch 计算程序所有时间

令狐掌门 发布时间:2021-02-13 10:45:23 ,浏览量:3

代码

/*
 
Stopwatch 计算程序所有时间
 
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace 计算程序运行时间
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            //ArrayList aList = new ArrayList();
            //for (int i = 0; i < 10000000; i++)
            //{
            //    aList.Add(i);
            //}
            //00:00:00.8874700

            List list = new List();
            for (int i = 0; i < 10000000; i++)
            {
                list.Add(i);
            }
            //00:00:00.0930832
            sw.Stop();

            //运行时间
            Console.WriteLine(sw.Elapsed);

            Console.ReadKey();
        }
    }
}

Stopwatch属于命名空间System.Diagnostics,程序运行时间最终返回的是TimeSpan,该类封装了一些相关属性,例如时分秒毫秒等,可以在代码中自由调用。

关注
打赏
1688896170
查看更多评论

令狐掌门

暂无认证

  • 3浏览

    0关注

    485博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0567s