文章出處

Stopwatch類:http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.100).aspx

 常用屬性和方法:

Start(): 開始或繼續測量某個時間間隔的運行時間。

Stop(): 停止測量某個時間間隔的運行時間。

ElapsedMilliseconds:獲取當前實例測量得出的總運行時間(以毫秒為單位)。

例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace BoxAndUnBox
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            long result = SumWithoutBox();
            watch.Stop();
            Console.WriteLine("SumWithoutBox()方法返回計算結果:{0},用時{1}毫秒",
               result, watch.ElapsedMilliseconds);//獲取當前實例測量得出的總運行時間(以毫秒為單位)
            watch.Restart();
            result = SumWithBox();
            watch.Stop();
            Console.WriteLine("SumWithBox()方法返回計算結果:{0},用時{1}毫秒",
         result, watch.ElapsedMilliseconds);
            Console.ReadKey();
        }

        static long SumWithoutBox()
        {
            long sum = 0;
            for (long i = 0; i < 10000000; i++)
                sum += i;
            return sum;
        }

        static long SumWithBox()
        {
            object sum = 0L; //裝箱
            for (long i = 0; i < 10000000; i++)
                sum = (long)sum + i;//先拆箱,求和,再裝箱
            return (long)sum;//拆箱
        }
    }
}

 

 

------------>>>

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()