我點擊快速排序,排序方法是對的但是消耗時間不顯示,請問是什么問題,請幫助





uj5u.com熱心網友回復:
用Stopwatch來計時:var 碼表 = System.Diagnostics.Stopwatch.StartNew();
// 耗時操作。。。
var 耗時毫秒 = 碼表.ElapsedMilliseconds;
uj5u.com熱心網友回復:
如果是想獲取每種排序方法消耗的時間的話,那肯定不是用timer控制元件,timer控制元件是每隔一定時間執行一項任務,應該用.NET內置的Stopwatch類。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 3, 2, 1 };
// 開始計時
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Reset();
watch.Start();
// 停止計時
SortMethodOne(nums);
watch.Stop();
Console.WriteLine("方法一所用時間為:{0}毫秒", watch.ElapsedMilliseconds);
// 重新計時
watch.Restart();
SortMethodTwo(nums);
watch.Stop();
Console.WriteLine("方法二所用時間為:{0}毫秒", watch.ElapsedMilliseconds);
}
static void SortMethodOne(int[] nums)
{
// 排序方法一耗時3000毫秒
System.Threading.Thread.Sleep(3000);
}
static void SortMethodTwo(int[] nums)
{
// 排序方法二耗時5000毫秒
System.Threading.Thread.Sleep(5000);
}
}
}
uj5u.com熱心網友回復:
首先,記錄當前時間然後操作你的排序
然後記下現在的時間
然後現在的時間-當前時間就是你消耗的時間
uj5u.com熱心網友回復:
非常感謝,
初學者,現在懂了,我試試
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/119068.html
標籤:C#
上一篇:深圳哪里缺人不?
下一篇:關于自動生成實驗的初始想法
