最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#計(jì)算一段程序運(yùn)行時(shí)間的多種方法總結(jié)

 更新時(shí)間:2025年10月22日 10:04:20   作者:xzjxylophone  
這篇文章主要為大家詳細(xì)介紹了C#計(jì)算一段程序運(yùn)行時(shí)間的多種方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(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)文章

最新評(píng)論

休宁县| 江都市| 安福县| 万载县| 会昌县| 长丰县| 富裕县| 谢通门县| 遂川县| 河北省| 宾阳县| 正镶白旗| 洛宁县| 子洲县| 永修县| 灵川县| 乌拉特前旗| 杨浦区| 北碚区| 子长县| 柏乡县| 东丽区| 巴楚县| 宕昌县| 稷山县| 香河县| 漠河县| 铜川市| 龙口市| 牡丹江市| 土默特右旗| 广丰县| 襄城县| 花莲县| 汝城县| 阜南县| 泸水县| 旌德县| 承德县| 泰顺县| 阿合奇县|