Click here to Skip to main content
13,248,087 members (40,669 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

20.4K views
494 downloads
15 bookmarked
Posted 28 May 2008

Comparative Speed Testing

, 28 May 2008
A simple-to-use class for performing comparative, non-benchmarked speed tests when optimising code for execution speed.
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections;
using ZED.Utility.Development;

/// <summary>
/// clsSpeedTestAB_Recursion - Speed test Stack-Push-Loop versus Recursive method calls.
/// </summary>
/// <overview>
/// </overview>
/// <remarks>
/// </remarks>
/// <notes>
/// </notes>
public class clsSpeedTestAB_Recursion : clsBaseSpeedTestAB
{
    protected const int kMAX = 100;

    /// <summary>
    /// Construct a new speed test with prescribed repetitions.
    /// </summary>
    /// <param name="aRepetitions"></param>
    /// <remarks></remarks>
    public clsSpeedTestAB_Recursion(int aRepetitions) : base(aRepetitions)
    {
    }

    /// <summary>
    /// Overriden speed test A setup.
    /// </summary>
    /// <remarks></remarks>
    protected override void SetUpTestA()
    {
        base.f_DescribeB = "Loop, calling a function recursively.";
    }

    /// <summary>
    /// Overriden speed test B setup.
    /// </summary>
    /// <remarks></remarks>
    protected override void SetUpTestB()
    {
        base.f_DescribeA = "Loop, pushing data on a stack.";
    }

    /// <summary>
    /// Overridden Speed Test A.
    /// </summary>
    /// <remarks></remarks>
    protected override void SpeedTestA()
    {
        this.TestA(0);
    }

    /// <summary>
    /// Overridden Speed Test B.
    /// </summary>
    /// <remarks></remarks>
    protected override void SpeedTestB()
    {
        this.TestB(0);
    }

    protected void TestA(int aNumber)
    {
        if (aNumber != 100)
        {
            this.TestA(aNumber + 1);
        }
    }

    protected void TestB(int aNumber)
    {
        Stack xStack = new Stack();
        bool xDecrease = false;
        int xMax = aNumber + 100;
        int xNumber = aNumber;
        while (true)
        {
            if (xDecrease)
            {
                xNumber = Conversions.ToInteger(xStack.Pop());
                if (xNumber != aNumber)
                {
                    continue;
                }
                break;
            }
            xStack.Push(xNumber);
            if (xNumber == xMax)
            {
                xDecrease = true;
            }
            else
            {
                xNumber++;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Warrick Procter
ZED
New Zealand New Zealand
A DEC PDP/11 BasicPlus2 developer from the 80s.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 29 May 2008
Article Copyright 2008 by Warrick Procter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid