Performance timer for profiling purposes. For a full description, please refer to http://thebuildingcoder.typepad.com/blog/2010/03/performance-profiling.html
Inheritance: IDisposable
        /// <summary>
        /// Benchmark the use of a parameter filter versus
        /// various kinds of post processing of the
        /// results returned by the filtered element
        /// collector to find the level specified by
        /// iLevel.
        /// </summary>
        void BenchmarkSpecificLevel( int iLevel )
        {
            Type t = typeof( Level );
              string name = "Level " + iLevel.ToString();
              Element level;

              using( JtTimer pt = new JtTimer(
            "Empty method *" ) )
              {
            level = EmptyMethod(
              t, name );
              }

              level = null;

              using( JtTimer pt = new JtTimer(
            "Collector with no name check *" ) )
              {
            level = GetFirstElementOfType( t );
              }

              Debug.Assert( null != level, "expected to find a valid level" );

              level = null;

              using( JtTimer pt = new JtTimer(
            "Parameter filter" ) )
              {
            //level = GetFirstElementOfTypeWithBipString(
            //  t, BuiltInParameter.ELEM_NAME_PARAM, name );

            level = GetFirstElementOfTypeWithBipString(
              t, BuiltInParameter.DATUM_TEXT, name );
              }

              Debug.Assert( null != level,
            "expected to find a valid level" );

              level = null;

              using( JtTimer pt = new JtTimer( "Explicit" ) )
              {
            level = GetFirstNamedElementOfTypeUsingExplicitCode(
              t, name );
              }

              Debug.Assert( null != level, "expected to find a valid level" );
              level = null;

              using( JtTimer pt = new JtTimer( "Linq" ) )
              {
            level = GetFirstNamedElementOfTypeUsingLinq(
              t, name );
              }

              Debug.Assert( null != level, "expected to find a valid level" );
              level = null;

              using( JtTimer pt = new JtTimer(
            "Anonymous named" ) )
              {
            level = GetFirstNamedElementOfTypeUsingAnonymousButNamedMethod(
              t, name );
              }

              Debug.Assert( null != level, "expected to find a valid level" );
              level = null;

              using( JtTimer pt = new JtTimer( "Anonymous" ) )
              {
            level = GetFirstNamedElementOfTypeUsingAnonymousMethod(
              t, name );
              }

              Debug.Assert( null != level, "expected to find a valid level" );
        }
        void RunBenchmark()
        {
            // Create a number of levels for us to play with:

              int maxLevel = 1000;
              for( int i = 3; i < maxLevel; ++i )
              {
            CreateLevel( i );
              }

              // Run a specified number of tests
              // to retrieve all levels in different
              // ways:

              int nLevels = GetElementsOfType( typeof( Level ) )
            .ToElements().Count;

              int nRuns = 1000;

              JtTimer totalTimer = new JtTimer(
            "TOTAL TIME" );

              using( totalTimer )
              {
            for( int i = 0; i < nRuns; ++i )
            {
              BenchmarkAllLevels( nLevels );
            }
              }

              totalTimer.Report( "Retrieve all levels:" );

              // Run a specified number of tests
              // to retrieve a randomly selected
              // specific level:

              nRuns = 1000;
              Random rand = new Random();
              totalTimer.Restart( "TOTAL TIME" );

              using( totalTimer )
              {
            for( int i = 0; i < nRuns; ++i )
            {
              int iLevel = rand.Next( 1, maxLevel );
              BenchmarkSpecificLevel( iLevel );
            }
              }

              totalTimer.Report(
            "Retrieve specific named level:" );
        }
        /// <summary>
        /// Benchmark several different approaches to
        /// using filtered collectors to retrieve
        /// all levels in the model,
        /// and measure the time required to
        /// create IList and List collections
        /// from them.
        /// </summary>
        void BenchmarkAllLevels( int nLevels )
        {
            Type t = typeof( Level );
              int n;

              using( JtTimer pt = new JtTimer(
            "Empty method *" ) )
              {
            EmptyMethod( t );
              }

              using( JtTimer pt = new JtTimer(
            "NotElementType *" ) )
              {
            FilteredElementCollector a
              = GetNonElementTypeElements();
              }

              using( JtTimer pt = new JtTimer(
            "NotElementType as IList *" ) )
              {
            IList<Element> a
              = GetNonElementTypeElements().ToElements();
            n = a.Count;
              }
              Debug.Assert( nLevels <= n,
            "expected to retrieve all non-element-type elements" );

              using( JtTimer pt = new JtTimer(
            "NotElementType as List *" ) )
              {
            List<Element> a = new List<Element>(
              GetNonElementTypeElements() );

            n = a.Count;
              }
              Debug.Assert( nLevels <= n,
            "expected to retrieve all non-element-type elements" );

              using( JtTimer pt = new JtTimer( "Explicit" ) )
              {
            List<Element> a
              = GetElementsOfTypeUsingExplicitCode( t );

            n = a.Count;
              }
              Debug.Assert( nLevels == n,
            "expected to retrieve all levels" );

              using( JtTimer pt = new JtTimer( "Linq" ) )
              {
            IEnumerable<Element> a =
              GetElementsOfTypeUsingLinq( t );

            n = a.Count<Element>();
              }
              Debug.Assert( nLevels == n,
            "expected to retrieve all levels" );

              using( JtTimer pt = new JtTimer(
            "Linq as List" ) )
              {
            List<Element> a = new List<Element>(
              GetElementsOfTypeUsingLinq( t ) );

            n = a.Count;
              }
              Debug.Assert( nLevels == n,
            "expected to retrieve all levels" );

              using( JtTimer pt = new JtTimer( "Collector" ) )
              {
            FilteredElementCollector a
              = GetElementsOfType( t );
              }

              using( JtTimer pt = new JtTimer(
            "Collector as IList" ) )
              {
            IList<Element> a
              = GetElementsOfType( t ).ToElements();

            n = a.Count;
              }
              Debug.Assert( nLevels == n,
            "expected to retrieve all levels" );

              using( JtTimer pt = new JtTimer(
            "Collector as List" ) )
              {
            List<Element> a = new List<Element>(
              GetElementsOfType( t ) );

            n = a.Count;
              }
              Debug.Assert( nLevels == n,
            "expected to retrieve all levels" );
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
              _doc = app.ActiveUIDocument.Document;

              // create a number of levels for us to play with:

              int maxLevel = 1000;
              for( int i = 3; i < maxLevel; ++i )
              {
            CreateLevel( i );
              }

              // run a specified number of tests
              // to retrieve all levels in different
              // ways:

              int nLevels = GetElementsOfType( typeof( Level ) )
            .ToElements().Count;

              int nRuns = 1000;

              JtTimer totalTimer = new JtTimer(
            "TOTAL TIME" );

              using( totalTimer )
              {
            for( int i = 0; i < nRuns; ++i )
            {
              BenchmarkAllLevels( nLevels );
            }
              }

              totalTimer.Report( "Retrieve all levels:" );

              // run a specified number of tests
              // to retrieve a randomly selected
              // specific level:

              nRuns = 1000;
              Random rand = new Random();
              totalTimer.Restart( "TOTAL TIME" );

              using( totalTimer )
              {
            for( int i = 0; i < nRuns; ++i )
            {
              int iLevel = rand.Next( 1, maxLevel );
              BenchmarkSpecificLevel( iLevel );
            }
              }

              totalTimer.Report(
            "Retrieve specific named level:" );

              return Result.Failed;
        }