C#計(jì)算一段程序運(yùn)行時(shí)間的多種方法總結(jié)
直接代碼:
第一種方法利用System.DateTime.Now
static void SubTest()
{
DateTime beforDT = System.DateTime.Now;
//耗時(shí)巨大的代碼
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Console.WriteLine("DateTime總共花費(fèi){0}ms.", ts.TotalMilliseconds);
}第二種用Stopwatch類(System.Diagnostics)
static void SubTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
//耗時(shí)巨大的代碼
sw.Stop();
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine("Stopwatch總共花費(fèi){0}ms.", ts2.TotalMilliseconds);
}第三種用API實(shí)現(xiàn):
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
static void SubTest()
{
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
//耗時(shí)巨大的代碼
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("QueryPerformanceCounter耗時(shí): {0} 秒", result);
}方法補(bǔ)充
C#中獲取程序執(zhí)行時(shí)間的三種方法
1. 使用DateTime類
你可以在程序開始執(zhí)行前獲取當(dāng)前時(shí)間,然后在程序結(jié)束時(shí)再次獲取當(dāng)前時(shí)間,通過(guò)這兩個(gè)時(shí)間點(diǎn)計(jì)算程序執(zhí)行時(shí)間。
using System;
class Program
{
static void Main()
{
DateTime startTime = DateTime.Now;
// 執(zhí)行你的代碼
for (int i = 0; i < 1000000; i++)
{
// 示例:一個(gè)簡(jiǎn)單的循環(huán)
}
DateTime endTime = DateTime.Now;
TimeSpan executionTime = endTime - startTime;
Console.WriteLine("程序執(zhí)行時(shí)間: " + executionTime.TotalMilliseconds + " 毫秒");
}
}2. 使用Stopwatch類
System.Diagnostics.Stopwatch類是測(cè)量時(shí)間的一種更精確的方法,特別是對(duì)于需要高精度計(jì)時(shí)的場(chǎng)景。
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 執(zhí)行你的代碼
for (int i = 0; i < 1000000; i++)
{
// 示例:一個(gè)簡(jiǎn)單的循環(huán)
}
stopwatch.Stop();
Console.WriteLine("程序執(zhí)行時(shí)間: " + stopwatch.ElapsedMilliseconds + " 毫秒");
}
}3. 使用Environment.TickCount或Environment.TickCount64(對(duì)于64位系統(tǒng))
這種方法不如Stopwatch精確,但對(duì)于簡(jiǎn)單的性能測(cè)試或快速獲取時(shí)間差也是可行的。
using System;
class Program
{
static void Main()
{
int startTime = Environment.TickCount; // 或使用Environment.TickCount64對(duì)于64位系統(tǒng)以避免溢出
// 執(zhí)行你的代碼
for (int i = 0; i < 1000000; i++)
{
// 示例:一個(gè)簡(jiǎn)單的循環(huán)
}
int endTime = Environment.TickCount; // 或使用Environment.TickCount64對(duì)于64位系統(tǒng)以避免溢出
int executionTime = endTime - startTime; // 注意:這將返回以毫秒為單位的整數(shù),但不直接提供TimeSpan對(duì)象。
Console.WriteLine("程序執(zhí)行時(shí)間: " + executionTime + " 毫秒");
}
}到此這篇關(guān)于C#計(jì)算一段程序運(yùn)行時(shí)間的多種方法總結(jié)的文章就介紹到這了,更多相關(guān)C#計(jì)算程序運(yùn)行時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法,主要通過(guò)接口ICustomTypeDescriptor實(shí)現(xiàn),需要的朋友可以參考下2014-09-09
C#中使用DataContractSerializer類實(shí)現(xiàn)深拷貝操作示例
這篇文章主要介紹了C#中使用DataContractSerializer類實(shí)現(xiàn)深拷貝操作示例,本文給出了實(shí)現(xiàn)深拷貝方法、測(cè)試深拷貝方法例子、DataContractSerializer類實(shí)現(xiàn)深拷貝的原理等內(nèi)容,需要的朋友可以參考下2015-06-06
C#中DataTable 轉(zhuǎn)實(shí)體實(shí)例詳解
這篇文章主要介紹了C#中DataTable 轉(zhuǎn)實(shí)體實(shí)例詳解,需要的朋友可以參考下2017-04-04
C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)帶進(jìn)度條的ListView
這篇文章主要介紹了C#實(shí)現(xiàn)帶進(jìn)度條的ListView 的相關(guān)資料,需要的朋友可以參考下2016-02-02
基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口
這篇文章主要為大家詳細(xì)介紹了如何使用C#寫一個(gè)接受圖片流的OCR識(shí)別接口,以及測(cè)試用例調(diào)用接口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

