您的位置:首页 > 编程语言 > C#

C#如何利用Stopwatch比较精确的测试算法运算时间

2009-02-18 19:13 615 查看
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test
{
class Program
{
static string[] m_Data = { "A", "B", "C", "D", "E", "A1", "B1" };
static void Main(string[] args)
{
GC.Collect();
Stopwatch watch = new Stopwatch();
using (new AutoWatch(watch))
{
Fe1();

}
Console.WriteLine(watch.ElapsedTicks);

}

static void Fe1()
{

List<string> list = new List<string>();
List<string> temp = new List<string>();
List<string> result = new List<string>();
int n = m_Data.Length;
string t;
string sEnd = m_Data[n - 1];
int i = 0;

while (i < n)
{
t = m_Data[i];
temp.Add(t);
foreach (string s in list)
{
//Console.WriteLine(t + s);
result.Add(t + s);
temp.Add(t + s);
}
list.AddRange(temp);
temp.Clear();
i++;
}

}

public sealed class AutoWatch : IDisposable
{
private Stopwatch watch;

public AutoWatch(Stopwatch watch)
{
this.watch = watch;
watch.Start();
}

void IDisposable.Dispose()
{
watch.Stop();
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: