測試程序運(yùn)行時(shí)間 (轉(zhuǎn))
原文: http://www.cnblogs.com/kavilee/archive/2010/12/13/1904274.html
一、用C#自帶的StopWatch函數(shù)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace StopWatch
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? Stopwatch sw = new Stopwatch();
??????????? sw.Start();
??????????? //這里填寫要執(zhí)行的代碼
??????????? sw.Stop();
??????????? Console.WriteLine("總運(yùn)行時(shí)間:" + sw.Elapsed);
??????????? Console.WriteLine("測量實(shí)例得出的總運(yùn)行時(shí)間(毫秒為單位):" + sw.ElapsedMilliseconds);
??????????? Console.WriteLine("總運(yùn)行時(shí)間(計(jì)時(shí)器刻度標(biāo)識(shí)):" + sw.ElapsedTicks);
??????????? Console.WriteLine("計(jì)時(shí)器是否運(yùn)行:" + sw.IsRunning.ToString());
??????? }
??? }
}
運(yùn)行結(jié)果如下:
總運(yùn)行時(shí)間:00:00:00.0000013
測量實(shí)例得出的程序運(yùn)行時(shí)間(毫秒為單位):0
總運(yùn)行時(shí)間(計(jì)時(shí)器刻度標(biāo)識(shí)):5
計(jì)時(shí)器是否運(yùn)行:False
?
二、用API函數(shù)QueryPerformanceFrequency
using ?System; ??
using ?System.Threading; ??
class ?Class1 ??
{ ??
????[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); ??
????[STAThread] ??
???? static ? void ?Main( string []?args) ??
????{ ??
???????? long ?count?=?0; ??
???????? long ?count1?=?0; ??
???????? long ?freq?=?0; ??
???????? double ?result?=?0; ??
????????QueryPerformanceFrequency( ref ?freq); ??
????????QueryPerformanceCounter( ref ?count); ??
???????? //需要測試的模塊 ??
??
???????? int ?heisetoufa; ??
???????? for ?(heisetoufa?=?1;?heisetoufa?<?10000;?heisetoufa++) ??
????????{ ??
????????????Console.WriteLine( "第" ?+?heisetoufa?+? "行" ); ??
???????????? if ?(heisetoufa?==?5000) ??
????????????{ ??
????????????????Thread.Sleep(10000); ??
????????????} ??
????????} ??
??
???????? //需要測試的模塊 ??
??
????????QueryPerformanceCounter( ref ?count1); ??
????????count?=?count1?-?count; ??
????????result?=?( double )(count)?/?( double )freq; ??
????????Console.WriteLine( "耗時(shí):?{0}?秒" ,?result); ??
????????Console.ReadLine(); ??
????} ??
}??
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
