/*
* Title : 运行速度测试 TestRunSpeed
* Author : MSDN WebCast 改
* Date : 3月22日
* Description : 用于测试Test1 和 Test2 的运行时间差距结果以百分比表式
* Environment: WinXp Sp2,Vs2005 Pro,.Net Framework 2.0
* KeyWord : Csharp Test Diagnostics Stopwatch delegate TestRunSpeed
*/
using System;
using SD=System.Diagnostics;
using SWF=System.Windows.Forms;
namespace Ascenta.ITTeam.Demo

{
public partial class Form1 : SWF.Form
{
delegate void TestMethod();
public Form1()
{
InitializeComponent();
}
void Test1()
{
long l=0;
for (long i = 1; i < tbNumber.Value; i++)
{
l = l + i;
}
}
void Test2()
{
long l = 0;
for (long i = 1; i < tbNumber.Value*100; i++)
{
l = l + i;
}
}

/**//// <summary>
/// 计算测试时间
/// </summary>
/// e.g.
/// float testresult1 = GetTestTime(Test1);
/// <param name="testMethod">运行的方法</param>
/// <returns>测试时间</returns>
long GetTestTime(TestMethod testMethod)
{
SD.Stopwatch stopper = new SD.Stopwatch();
stopper.Start();
testMethod();
stopper.Stop();
return stopper.ElapsedMilliseconds;
}
private void btnTest_Click(object sender, EventArgs e)
{
float testresult1 = GetTestTime(Test1);
float testresult2 = GetTestTime(Test2);
txtResult.Text = (Convert.ToString((testresult1 / testresult2) * 100)) + "%";
}

}
}
运行速度测试 TestRunSpeed
最新推荐文章于 2023-12-26 17:39:11 发布
本文介绍了一个简单的测试案例,用于比较两个不同方法(Test1和Test2)的运行速度,并通过百分比来展示它们之间的性能差异。测试使用了.NET Framework 2.0环境下的Stopwatch组件来准确测量每个方法的执行时间。
3067

被折叠的 条评论
为什么被折叠?



