C#中Stopwatch的使用及說(shuō)明
C# Stopwatch的使用
什么是Stopwatch
Stopwatch:提供一組方法和屬性,可以準(zhǔn)確的測(cè)量運(yùn)行時(shí)間。
使用的時(shí)候需要引用命名空間:System.Diagnostics。
Stopwatch的簡(jiǎn)單使用
//創(chuàng)建Stopwatch實(shí)例 Stopwatch sw = new Stopwatch(); //開始計(jì)時(shí) sw.Start(); for (int i = 0; i < 100; i++) { ? Console.WriteLine(i); } //停止計(jì)時(shí) sw.Stop(); Console.WriteLine("用時(shí):" + sw.ElapsedMilliseconds + ""); //重置 停止時(shí)間間隔測(cè)量,并將運(yùn)行時(shí)間重置為0 sw.Reset(); Console.WriteLine("用時(shí):" + sw.ElapsedMilliseconds + ""); //重啟 停止時(shí)間間隔測(cè)量,并將運(yùn)行時(shí)間重置為0,然后重新開始測(cè)量運(yùn)行時(shí)間 sw.Restart(); for (int i = 0; i < 100; i++) { ? Console.WriteLine(i); } sw.Stop(); //獲取當(dāng)前實(shí)例測(cè)量得出的總運(yùn)行時(shí)間(以毫秒為單位) Console.WriteLine("用時(shí):" + sw.ElapsedMilliseconds + ""); //獲取當(dāng)前實(shí)例測(cè)量得出的總運(yùn)行時(shí)間 Console.WriteLine("用時(shí):"+sw.Elapsed);? //獲取當(dāng)前實(shí)例測(cè)量得出的總運(yùn)行時(shí)間(用計(jì)時(shí)器刻度表示)。 Console.WriteLine(sw.ElapsedTicks); Console.Read();
//使用StartNew,相當(dāng)于已經(jīng)實(shí)例化并且啟動(dòng)計(jì)時(shí) Stopwatch sw=Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { ? Console.WriteLine(i); } sw.Stop(); //獲取當(dāng)前實(shí)例測(cè)量得出的總運(yùn)行時(shí)間(以毫秒為單位) Console.WriteLine("用時(shí):" + sw.ElapsedMilliseconds + ""); //獲取當(dāng)前實(shí)例測(cè)量得出的總運(yùn)行時(shí)間 Console.WriteLine("用時(shí):"+sw.Elapsed);? Console.Read();
C#使用Stopwatch精確測(cè)量運(yùn)行時(shí)間
一般測(cè)量時(shí)間間隔使用的是DateTime.Now實(shí)例的DateTime.Ticks當(dāng)前屬性,想要精確測(cè)量一個(gè)操作的運(yùn)行時(shí)間就只能使用Stopwatch類計(jì)時(shí)了。
Stopwatch計(jì)時(shí)精度取決于硬件,如果安裝的硬件和操作系統(tǒng)支持高分辨率性能計(jì)數(shù)器, 則Stopwatch類將使用該計(jì)數(shù)器來(lái)測(cè)量運(yùn)行時(shí)間。否則,Stopwatch類將使用系統(tǒng)計(jì)時(shí)器來(lái)測(cè)量運(yùn)行時(shí)間。
測(cè)量耗時(shí)操作的運(yùn)行時(shí)間
? ? ? ? ? ? Stopwatch stopWatch = new Stopwatch(); ? ? ? ? ? ? stopWatch.Start(); ? ? ? ? ? ? Thread.Sleep(5000); // 耗時(shí)操作 ? ? ? ? ? ? stopWatch.Stop(); ? ? ? ? ? ?? ? ? ? ? ? ? // 將經(jīng)過(guò)的時(shí)間作為TimeSpan值 ? ? ? ? ? ? TimeSpan ts = stopWatch.Elapsed; ? ? ? ? ? ? // 格式和顯示時(shí)間值 ? ? ? ? ? ? string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}-{4:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds, (ts.Ticks * 100 / 1000)%1000); ? ? ? ? ? ? Console.WriteLine("RunTime " + elapsedTime); ? ? ? ? ? ? // 將經(jīng)過(guò)的時(shí)間作為毫秒數(shù) ? ? ? ? ? ? long mSeconds = stopWatch.ElapsedMilliseconds; ? ? ? ? ? ? Console.WriteLine("RunTime(ms) " + mSeconds); ? ? ? ? ? ? // 獲取經(jīng)過(guò)時(shí)間的計(jì)時(shí)器刻度 ? ? ? ? ? ? // 也可在耗時(shí)操作前后使用Stopwatch.GetTimestamp()各獲取1個(gè)Ticks值,然后相減得到耗時(shí)操作花費(fèi)的計(jì)時(shí)器刻度 ? ? ? ? ? ? // 計(jì)時(shí)器采用的計(jì)時(shí)方式不同,tick的時(shí)間單位不同 ? ? ? ? ? ? long tick = stopWatch.ElapsedTicks; ? ? ? ? ? ? Console.WriteLine("RunTime(tick) " + tick); ? ? ? ? ? ? if (Stopwatch.IsHighResolution) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // 計(jì)時(shí)器刻度是高性能計(jì)時(shí)器滴答數(shù) ? ? ? ? ? ? ? ? Console.WriteLine("使用系統(tǒng)高分辨率性能計(jì)數(shù)器計(jì)時(shí):"); ? ? ? ? ? ? ? ? Console.WriteLine(" ?RunTime(ns) " +tick* ((1000L * 1000L * 1000L)/ Stopwatch.Frequency)); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // 計(jì)時(shí)器刻度是DateTime.Now實(shí)例的DateTime.Ticks當(dāng)前屬性 ? ? ? ? ? ? ? ? Console.WriteLine("使用DateTime類計(jì)時(shí):"); ? ? ? ? ? ? ? ? Console.WriteLine(" ?RunTime(ns) " + tick * 100); ? ? ? ? ? ? }
查看Stopwatch計(jì)時(shí)器的計(jì)時(shí)方式
? ? ? ? /// <summary> ? ? ? ? /// 顯示計(jì)時(shí)器屬性 ? ? ? ? /// </summary> ? ? ? ? public static void DisplayTimerProperties() ? ? ? ? { ? ? ? ? ? ? // 顯示定時(shí)器頻率和分辨率 ? ? ? ? ? ? if (Stopwatch.IsHighResolution) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("操作使用系統(tǒng)高分辨率性能計(jì)數(shù)器計(jì)時(shí)"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("操作使用DateTime類計(jì)時(shí)"); ? ? ? ? ? ? } ? ? ? ? ? ? long frequency = Stopwatch.Frequency; ? ? ? ? ? ? Console.WriteLine(" ?計(jì)時(shí)器頻率,單位為每秒滴答數(shù) = {0}", ? ? ? ? ? ? ? ? frequency); ? ? ? ? ? ? long nanosecPerTick = (1000L * 1000L * 1000L) / frequency; ? ? ? ? ? ? Console.WriteLine(" ?計(jì)時(shí)器分辨率為 {0} 納秒/滴答", ? ? ? ? ? ? ? ? nanosecPerTick); ? ? ? ? }
附上官網(wǎng)上的一個(gè)測(cè)試實(shí)例
? ? ? ? private static void TimeOperations() ? ? ? ? { ? ? ? ? ? ? long nanosecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency; ? ? ? ? ? ? const long numIterations = 10000; ? ? ? ? ? ? // 定義操作標(biāo)題名稱 ? ? ? ? ? ? String[] operationNames = {"操作: Int32.Parse(\"0\")", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"操作: Int32.TryParse(\"0\")", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"操作: Int32.Parse(\"a\")", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"操作: Int32.TryParse(\"a\")"}; ? ? ? ? ? ? Console.WriteLine(); ? ? ? ? ? ? Console.WriteLine("注:1ticks=100ns,1s=1000ms,1ms=1000us,1us=1000ns"); ? ? ? ? ? ? // 從字符串解析整數(shù)的四種不同實(shí)現(xiàn) ? ? ? ? ? ? for (int operation = 0; operation <= 3; operation++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // 定義操作統(tǒng)計(jì)的變量 ? ? ? ? ? ? ? ? long numTicks = 0; ? ? ? ? ? ? ? ? long numRollovers = 0; ? ? ? ? ? ? ? ? long maxTicks = 0; ? ? ? ? ? ? ? ? long minTicks = Int64.MaxValue; ? ? ? ? ? ? ? ? int indexFastest = -1; ? ? ? ? ? ? ? ? int indexSlowest = -1; ? ? ? ? ? ? ? ? long milliSec = 0; ? ? ? ? ? ? ? ? Stopwatch time10kOperations = Stopwatch.StartNew(); ? ? ? ? ? ? ? ? // 運(yùn)行當(dāng)前操作10001次。 ? ? ? ? ? ? ? ? // 第一次執(zhí)行時(shí)間將被丟棄,因?yàn)樗赡軙?huì)扭曲平均時(shí)間。 ? ? ? ? ? ? ? ? for (int i = 0; i <= numIterations; i++) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? long ticksThisTime = 0; ? ? ? ? ? ? ? ? ? ? int inputNum; ? ? ? ? ? ? ? ? ? ? Stopwatch timePerParse; ? ? ? ? ? ? ? ? ? ? switch (operation) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? case 0: ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 使用try-catch語(yǔ)句分析有效整數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 啟動(dòng)新的秒表計(jì)時(shí)器 ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse = Stopwatch.StartNew(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = Int32.Parse("0"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch (FormatException) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 停止計(jì)時(shí)器,并保存操作所用的計(jì)時(shí)ticks ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse.Stop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ticksThisTime = timePerParse.ElapsedTicks; ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse = Stopwatch.StartNew(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!Int32.TryParse("0", out inputNum)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse.Stop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ticksThisTime = timePerParse.ElapsedTicks; ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse = Stopwatch.StartNew(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = Int32.Parse("a"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch (FormatException) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse.Stop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ticksThisTime = timePerParse.ElapsedTicks; ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse = Stopwatch.StartNew(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!Int32.TryParse("a", out inputNum)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inputNum = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? timePerParse.Stop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ticksThisTime = timePerParse.ElapsedTicks; ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? // 跳過(guò)第一個(gè)操作的時(shí)間,以防它導(dǎo)致一次性性能下降。 ? ? ? ? ? ? ? ? ? ? if (i == 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? time10kOperations.Reset(); ? ? ? ? ? ? ? ? ? ? ? ? time10kOperations.Start(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? // 更新迭代1-10001的操作統(tǒng)計(jì)信息。 ? ? ? ? ? ? ? ? ? ? ? ? if (maxTicks < ticksThisTime) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? indexSlowest = i; ? ? ? ? ? ? ? ? ? ? ? ? ? ? maxTicks = ticksThisTime; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (minTicks > ticksThisTime) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? indexFastest = i; ? ? ? ? ? ? ? ? ? ? ? ? ? ? minTicks = ticksThisTime; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? numTicks += ticksThisTime; ? ? ? ? ? ? ? ? ? ? ? ? if (numTicks < ticksThisTime) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Keep track of rollovers. ? ? ? ? ? ? ? ? ? ? ? ? ? ? numRollovers++; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 顯示10000次迭代的統(tǒng)計(jì)信息 ? ? ? ? ? ? ? ? time10kOperations.Stop(); ? ? ? ? ? ? ? ? milliSec = time10kOperations.ElapsedMilliseconds; ? ? ? ? ? ? ? ? Console.WriteLine(); ? ? ? ? ? ? ? ? Console.WriteLine("{0} 統(tǒng)計(jì):", operationNames[operation]); ? ? ? ? ? ? ? ? Console.WriteLine(" ?最慢時(shí)間: ?第{0}/{1}次操作,時(shí)間為{2} ticks", ? ? ? ? ? ? ? ? ? ? indexSlowest, numIterations, maxTicks); ? ? ? ? ? ? ? ? Console.WriteLine(" ?最快時(shí)間: ?第{0}/{1}次操作,時(shí)間為{2} ticks", ? ? ? ? ? ? ? ? ? ? indexFastest, numIterations, minTicks); ? ? ? ? ? ? ? ? Console.WriteLine(" ?平均時(shí)間: ?{0} ticks = {1} ns", ? ? ? ? ? ? ? ? ? ? numTicks / numIterations, ? ? ? ? ? ? ? ? ? ? (numTicks * nanosecPerTick) / numIterations); ? ? ? ? ? ? ? ? Console.WriteLine(" ?{0} 次操作的總時(shí)間: {1} ms", ? ? ? ? ? ? ? ? ? ? numIterations, milliSec); ? ? ? ? ? ? } ? ? ? ? }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器的示例詳解
以往一般都是用 System.Timers.Timer 來(lái)做計(jì)時(shí)器,其實(shí) System.Threading.Timer 也可以實(shí)現(xiàn)計(jì)時(shí)器功能,下面就跟隨小編一起來(lái)學(xué)習(xí)一下如何使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器功能吧2024-01-01C#中使用DevExpress中的ChartControl實(shí)現(xiàn)極坐標(biāo)圖的案例詳解
這篇文章主要介紹了在C#中使用DevExpress中的ChartControl實(shí)現(xiàn)極坐標(biāo)圖,本案例是使用的是DevExpress 18.1.3版本,之前在14版本上也試過(guò),但是有一個(gè)弊端就是實(shí)現(xiàn)極坐標(biāo)圖的時(shí)候,第一個(gè)點(diǎn)和最后一個(gè)點(diǎn)總是自動(dòng)多一條閉合線,會(huì)形成一個(gè)閉合的多邊形,因此升級(jí)了一下版2022-02-02C#中Trim()、TrimStart()、TrimEnd()的用法介紹
這篇文章主要介紹了C#中Trim()、TrimStart()、TrimEnd()的用法,有需要的朋友可以參考一下2014-01-01DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法,對(duì)C#程序設(shè)計(jì)人員來(lái)說(shuō)是一個(gè)很實(shí)用的技巧,需要的朋友可以參考下2014-08-08

關(guān)于Flyweight模式應(yīng)用實(shí)踐的相關(guān)介紹

c#使用filesystemwatcher實(shí)時(shí)監(jiān)控文件目錄的添加和刪除