C#?Stopwatch實(shí)現(xiàn)計(jì)算代碼運(yùn)行時(shí)間
前言
我們編寫程序時(shí),有時(shí)需要統(tǒng)計(jì)代碼運(yùn)行時(shí)間,比如記錄視頻解碼耗時(shí),以及視頻的播放幀率等以確認(rèn)性能滿足,或者記錄代碼運(yùn)行時(shí)長作為優(yōu)化的依據(jù)。通常的做法是定義一個(gè)變量記錄起始時(shí)間,在結(jié)束時(shí)獲取當(dāng)前時(shí)間減去起始時(shí)間,本文將上述操作封裝為一些對象方便使用。
一、計(jì)算范圍時(shí)間
我們使用Stopwatch就很容易做到。
1、起始位置
Stopwatch _sw = Stopwatch.StartNew(); _sw.Start();
2、結(jié)束位置
_sw.Stop();
3、獲取時(shí)間
//單位秒 Time = _sw.ElapsedMilliseconds / 1000.0;
封裝成對象
(1)完整代碼
public class RangeCodeTime
{
Stopwatch _sw = Stopwatch.StartNew();
Queue<double> _times = new Queue<double>();
double _sum = 0;
int _maxAvgCount = 0;
/// <summary>
/// 當(dāng)前耗時(shí)時(shí),單位秒
/// </summary>
public double Time { get; set; }
/// <summary>
/// 平均耗時(shí)時(shí),單位秒
/// </summary>
public double Average { get; set; }
/// <summary>
/// 構(gòu)造方法
/// </summary>
/// <param name="maxAvgCount">計(jì)算平均總共個(gè)數(shù)</param>
public RangeCodeTime(int maxAvgCount = 60)
{
_maxAvgCount = maxAvgCount;
}
/// <summary>
/// 開始點(diǎn)
/// </summary>
public void Begin()
{
_sw.Restart();
}
/// <summary>
/// 結(jié)束點(diǎn)
/// </summary>
/// <param name="isPrint">是否打印</param>
/// <param name="printLable">打印的標(biāo)簽</param>
public void End(bool isPrint = true, string printLable = "")
{
_sw.Stop();
Time = _sw.ElapsedMilliseconds / 1000.0;
_times.Enqueue(Time);
_sum += Time;
Average = _sum / _times.Count;
Console.WriteLine(printLable + "當(dāng)前耗時(shí)(s):" + Time + " 平均耗時(shí)(s):" + Average);
if (_times.Count >= _maxAvgCount)
{
_sum -= _times.Dequeue();
}
}
}(2)使用示例
RangeCodeTime rct = new RangeCodeTime();
void test()
{
rct.Beigin();
//需要計(jì)算時(shí)長的代碼
//默認(rèn)會(huì)輸出到控制臺(tái),isPrint=true。也可以在End()之后通過屬性Time、和Average獲取數(shù)據(jù)。
rct.End(printLable: "解碼");
}
效果預(yù)覽

二、計(jì)算檢查點(diǎn)時(shí)間
檢查點(diǎn)通??梢杂糜谟?jì)算循環(huán)或者回調(diào)的耗時(shí),比如播放視頻過程中放置一個(gè)檢查點(diǎn),就可以計(jì)算出幀率。我們還是使用Stopwatch來實(shí)現(xiàn)。
1、初始化
Stopwatch _sw = Stopwatch.StartNew();
2、檢查點(diǎn)
if (!_sw.IsRunning)
{
_sw.Restart();
}
else
{
_sw.Stop();
//單位秒
Time = _sw.ElapsedMilliseconds / 1000.0;
_sw.Restart();
}
封裝成對象
(1)完整代碼
public class CheckPointCodeTime
{
Stopwatch _sw = Stopwatch.StartNew();
Queue<double> _times = new Queue<double>();
double _sum = 0;
int _maxAvgCount = 0;
/// <summary>
/// 當(dāng)前耗時(shí)時(shí),單位秒
/// </summary>
public double Time { get; set; }
/// <summary>
/// 平均耗時(shí)時(shí),單位秒
/// </summary>
public double Average { get; set; }
/// <summary>
/// 構(gòu)造方法
/// </summary>
/// <param name="maxAvgCount">計(jì)算平均總共個(gè)數(shù)</param>
public CheckPointCodeTime(int maxAvgCount = 60)
{
_maxAvgCount = maxAvgCount;
}
/// <summary>
/// 檢查點(diǎn)
/// </summary>
/// <param name="isPrint">是否打印</param>
/// <param name="printLable">打印的標(biāo)簽</param>
public void Check(bool isPrint = true, string printLable = "")
{
if (!_sw.IsRunning)
{
_sw.Restart();
return;
}
_sw.Stop();
Time = _sw.ElapsedMilliseconds / 1000.0;
_times.Enqueue(Time);
_sum += Time;
Average = _sum / _times.Count;
Console.WriteLine(printLable + "當(dāng)前耗時(shí)(s):" + Time + " 平均耗時(shí)(s):" + Average);
if (_times.Count >= _maxAvgCount)
{
_sum -= _times.Dequeue();
}
_sw.Restart();
}
}(2)使用示例
CheckPointCodeTime cct= new CheckPointCodeTime();
//接收視頻數(shù)據(jù)包回調(diào)
void onReceivePacket()
{
//默認(rèn)會(huì)輸出到控制臺(tái),isPrint=true。也可以在Check()之后通過屬性Time、和Average獲取數(shù)據(jù)。
cct.Check(printLable:"接收一幀 ");
//其他處理
}
效果預(yù)覽

總結(jié)
本文簡單的Stopwatch進(jìn)行了一個(gè)封裝,主要目的是方便調(diào)用,而且也將均值計(jì)算出來了,這樣有利于對數(shù)據(jù)的統(tǒng)計(jì),總的來說還是有一定適用場景的。
到此這篇關(guān)于C# Stopwatch實(shí)現(xiàn)計(jì)算代碼運(yùn)行時(shí)間的文章就介紹到這了,更多相關(guān)C# Stopwatch計(jì)算代碼運(yùn)行時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#中高精度計(jì)時(shí)器Stopwatch的用法詳解
- C#使用Stopwatch實(shí)現(xiàn)計(jì)時(shí)功能
- C#中Stopwatch的使用及說明
- C# 中使用Stopwatch計(jì)時(shí)器實(shí)現(xiàn)暫停計(jì)時(shí)繼續(xù)計(jì)時(shí)功能
- 如何使用C# Stopwatch 測量微秒級精確度
- .NET/C# 使用Stopwatch測量運(yùn)行時(shí)間
- C#使用StopWatch獲取程序毫秒級執(zhí)行時(shí)間的方法
- C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
- C#中的Timer和DispatcherTimer使用實(shí)例
- C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹
- C#中三種Timer計(jì)時(shí)器的詳細(xì)用法
- 詳解C#中的定時(shí)器Timer類及其垃圾回收機(jī)制
- C#使用timer實(shí)現(xiàn)的簡單鬧鐘程序
- [C#].NET中幾種Timer的使用實(shí)例
- C# 中Stopwatch和timer的實(shí)現(xiàn)示例
相關(guān)文章
C#中HslCommunication庫的實(shí)現(xiàn)示例
C# HslCommunication庫是一個(gè)用于建立TCP連接并進(jìn)行Modbus通訊的庫,使用該庫可以方便地建立TCP連接,并進(jìn)行讀寫操作,下面就來詳細(xì)的介紹一下,感興趣的可以了解一下2025-12-12
C#百萬數(shù)據(jù)查詢出現(xiàn)超時(shí)問題的解決方法
這篇文章主要介紹了C#百萬數(shù)據(jù)查詢出現(xiàn)超時(shí)問題的解決方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼
迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試2022-03-03
C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
這篇文章主要介紹了C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能,結(jié)合實(shí)例形式分析了C#使用QRCode動(dòng)態(tài)生成二維碼圖片相關(guān)操作技巧,需要的朋友可以參考下2019-02-02

