国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

用C#描述數(shù)據(jù)結(jié)構(gòu)1:統(tǒng)計代碼執(zhí)行時間對象

2019-11-06 06:44:17
字體:
供稿:網(wǎng)友

作者:vuefine 文獻(xiàn):

Data Structures and Algorithms Using C# |Michael Mcmillan參考博客:http://www.th7.cn/PRogram/net/201410/295545.shtml

平臺:.NET 2.0+


  自定義一個統(tǒng)計代碼執(zhí)行時間的類CalcTiming,考慮了:

在統(tǒng)計執(zhí)行過程中,不允許有垃圾回收。即在統(tǒng)計代碼執(zhí)行時間之前,就讓GC完成垃圾回收。

確保統(tǒng)計的是當(dāng)前進(jìn)程中代碼的執(zhí)行時間。

編寫代碼如下:

public class CalcTiming { TimeSpan startTime; private TimeSpan endTime; TimeSpan duration; public CalcTiming() { startTime = new TimeSpan(0); duration = new TimeSpan(0); } public void StartTime() { //強(qiáng)制對所有代進(jìn)行垃圾回收 GC.Collect(); //掛起線程,終結(jié)器線程清空該隊列為止 GC.WaitForPendingFinalizers(); //用戶運(yùn)行代碼的起始時間 startTime = Process.GetCurrentProcess().UserProcessorTime; } //統(tǒng)計的代碼執(zhí)行完畢的時間 public void StopTime() { endTime = Process.GetCurrentProcess().UserProcessorTime; duration = endTime.Subtract(startTime); } //返回統(tǒng)計的代碼執(zhí)行消耗的時間 public TimeSpan Result() { return duration; } }

  運(yùn)用以上CalcTiming類,統(tǒng)計顯示數(shù)組的消耗時間:

class Program { static void Main(string[] args) { int[] nums = new int[100000]; BuildArray(nums); CalcTiming calcTime= new CalcTiming(); calcTime.StartTime(); DisplayNums(nums); calcTime.StopTime(); Console.WriteLine(string.Format("共用時間:{0} ms",calcTime.Result().TotalMilliseconds)); Console.Read(); } static void BuildArray(int[] arr) { for (int i = 0; i < 100000; i++) arr[i] = 1; } static void DisplayNums(int[] arr) { for (int i = 0; i <= arr.GetUpperBound(0); i++) Console.Write(arr[i] + " "); } }

  總結(jié),在接下來用C#描述所有數(shù)據(jù)結(jié)構(gòu),統(tǒng)計消耗的時間時,采用CalcTiming對象。   源碼下載地址:   http://download.csdn.net/detail/daigualu/9771778


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 闽清县| 晋城| 太谷县| 宁津县| 海原县| 进贤县| 德兴市| 锦州市| 宿州市| 涡阳县| 温泉县| 德阳市| 溧水县| 德昌县| 黄冈市| 天峨县| 遵义县| 清涧县| 永德县| 乌什县| 竹北市| 铜川市| 万盛区| 华安县| 循化| 平南县| 建湖县| 图片| 南昌县| 泰宁县| 五寨县| 赣州市| 名山县| 于田县| 泽普县| 雷州市| 河津市| 逊克县| 汉中市| 石狮市| 巫溪县|