运行速度测试 TestRunSpeed

来源:互联网 发布:淘宝怎么改成差评 编辑:程序博客网 时间:2024/05/02 01:21

   /*
 * 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)) + "%";
        }



    }

}

原创粉丝点击