Click here to Skip to main content
15,885,806 members
Articles / Programming Languages / XSLT

Big O Algorithm Analyzer for .NET

,
Rate me:
Please Sign up or sign in to vote.
4.88/5 (20 votes)
16 Feb 2010CPOL10 min read 115.9K   1.7K   78  
A heurisitc graphing tool to help discover 'Big O Notation' function thru infinite asymptotic's and instrumentation.
using System;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Performance;
namespace WPFBigOAnalyzer
{
    class AnalysisFactory
    {
        #region Properties and Fields

        private Assembly _asm;

        public static double Min { get; set; }

        public static double Max { get; set; }
    
        protected internal Assembly TargetAssembly
        {
            get { return _asm; }
            set { _asm = value; }
        }
        private Type _targetClass;

        protected internal Type TargetClass
        {
            get { return _targetClass; }
            set { _targetClass = value; }
        }

        private Object _entryPoint;
        private MethodInfo _entryPointMethod;
        private PropertyInfo _entryPointProperty;

        protected internal Object EntryPoint
        {
            get { return _entryPoint; }
            set 
            {
                _entryPoint = value;
                if (_entryPoint.GetType().FullName.IndexOf("System.Reflection.RuntimeMethodInfo") != -1)
                    _entryPointMethod = (MethodInfo)value;
                else if (_entryPoint.GetType().FullName.IndexOf("System.Reflection.RuntimePropertyInfo") != -1)
                    _entryPointProperty = (PropertyInfo)value;
                else
                    throw new ArgumentException("You have selected an invalid entry point...");
            }
        }

        private int _infinityPoint;

        public int InfinityPoint
        {
            get { return _infinityPoint; }
            set { _infinityPoint = value; }
        }

        #endregion

        #region Object factory methods

        protected internal Type[] LoadAssembly(String AssemblyName)
        {
            try
            {
                this.TargetAssembly = Assembly.LoadFile(AssemblyName);
            }
            catch (Exception e)
            {
                throw new Exception("Unhandled Exception in 'AnalysisFactory.LoadAssembly()'.", e);
            }
            return TargetAssembly.GetTypes();
        }

        protected internal MethodInfo[] LoadMethodType(Type TheClass)
        {
            try
            {
                this.TargetClass = TheClass;
            }
            catch (Exception e)
            {
                throw new Exception("Unhandled Exception in '[MethodInfo] AnalysisFactory.LoadType()'.", e);
            }
            return this.TargetClass.GetMethods();
        }

        protected internal PropertyInfo[] LoadPropertyType(Type TheClass)
        {
            try 
            {
                this.TargetClass = TheClass;
            }
            catch (Exception e)
            {
                throw new Exception("Unhandled Exception in '[PropertyInfo] AnalysisFactory.LoadType()'.", e);
            }
            return this.TargetClass.GetProperties();
        }

        protected internal void LoadEntryPoint(MethodInfo TheMethod)
        {
            this.EntryPoint = TheMethod;
        }

        protected internal void LoadEntryPoint(PropertyInfo TheProperty)
        {
            this.EntryPoint = TheProperty;
        }

        #endregion

        #region Analysis factory methods

        private Stopwatch _sw;
        private double _theCount;
        /*
         * RunAnalysis
         */
        protected internal void RunAnalysis(bool InfiniteAsy)
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            _timeIndex = 0;
            _theCount = 0;

            object[] param = new object[1];

            if (this._asm != null && this._entryPoint != null && this._targetClass != null && this._infinityPoint != 0)
            {
                _markerPoints = new object[this._infinityPoint];

                if (this._sw == null)
                    this._sw = new Stopwatch();
                else
                    this._sw.Reset();

                this._sw.Start();
                this._sw.Stop();
                _markerPoints[0] = MarkerPoint;

                //Invoking a method..
                if (this._entryPoint.GetType().FullName.IndexOf("System.Reflection.RuntimeMethodInfo") != -1 && InfiniteAsy)
                {
                    
                    for (this._theCount = 1; this._theCount < _infinityPoint; this._theCount++)
                    {
                        param[0] = this._theCount;
                        
                        this._sw.Start();
                        this._entryPointMethod.Invoke(this._targetClass, param);

                        this._sw.Stop();
                        System.GC.WaitForPendingFinalizers();
                        _markerPoints[(int)this._theCount] = MarkerPoint;
                        
                        if (Min > MarkerPoint[0])
                            Min = MarkerPoint[0];
                        if (Max < MarkerPoint[0])
                            Max = MarkerPoint[0];
                    }
                }
                //Invoking a property...
                else if (this._entryPoint.GetType().FullName.IndexOf("System.Reflection.RuntimePropertyInfo") != -1 && InfiniteAsy)
                {
                    object target = this._asm.CreateInstance(this._targetClass.FullName);
                    

                    for (this._theCount = 1; this._theCount < _infinityPoint; this._theCount++)
                    {
                        this._sw.Start();
                        this._entryPointProperty.SetValue(target, this._theCount, null);

                        this._sw.Stop();
                        System.GC.WaitForPendingFinalizers();
                        _markerPoints[(int)this._theCount] = MarkerPoint;
                    }
                }
                //invoking a method for instrimenation...
                else if (this._entryPoint.GetType().FullName.IndexOf("System.Reflection.RuntimeMethodInfo") != -1 && !InfiniteAsy)
                {
                    
                    
                    //Don't run garbage collector during run, may need to impliment custom garbage collection.
                    object thisObj = this._asm.CreateInstance(this._targetClass.FullName);
                    System.GC.KeepAlive(thisObj);

                    //must impliment custom markers for instrimentation
                    this._sw.Start();
                    this._entryPointMethod.Invoke(thisObj, null);
                    this._sw.Stop();

                    System.GC.ReRegisterForFinalize(thisObj);
                }
            }
            else
                throw new Exception("Bad analysis! in 'AnalysisFactory.RunAnalysis()'");
            
            Thread.CurrentThread.Priority = ThreadPriority.Normal;
        }

        private double _timeIndex = 0, _pointIndex = 0;
        public double[] MarkerPoint
        {
            get
            {
                _timeIndex = this._sw.GetSplitTimeInMicroseconds();
                _pointIndex = _theCount;
                return new double[] { _timeIndex, _pointIndex }; 
            }
        }

        private object[] _markerPoints;
        protected internal object[] MarkerPoints
        {
            get { return _markerPoints; }
            set { _markerPoints = value; }
        }

        public DecoratedMarker DecoratedMarker
        {
            get
            {
                return new DecoratedMarker(MarkerPoint, "", null);
            }
        }
        #endregion
    }

    public class DecoratedMarker
    {
        private double[] _point;

        protected internal double[] Point
        {
            get { return _point; }
            set { _point = value; }
        }

        private String _markerText;

        public String MarkerText
        {
            get { return _markerText; }
            set { _markerText = value; }
        }

        private object _decorator;

        public object Decorator
        {
            get { return _decorator; }
            set { _decorator = value; }
        }
        protected internal DecoratedMarker(double[] Point, String MarkerText, object Decorator)
        {
            this._point = Point;
            this._markerText = MarkerText;
            this._decorator = Decorator;
        }
    }
}

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)


Written By
United States United States
I am a .NET developer who works daily on enterprise-scale applications using C#, SQL, XML, ASP.NET, and myriad other technologies. My academic background is in physics and economics.

I am the original architect of Sandcastle managed reference documentation engine and of the Meta.Numerics library for scientific computation.

Written By
CEO AW Proto-Code, Inc.
United States United States
I’m a self learned wiz kid turned Architect. Stared with an Apple IIe, using AppleSoft Basic, ProDos and Begal Basic at age 10.

Picked up LPC in college before the Dot Com Explosion. Wrote some Object C in the USAF for one of my instructors. Got a break from a booming computer manufacture testing server software. Left the Boom and went to work for a Dot Com built from the ashes of Sun Micro in CS. Mentoring in Java solidified me as a professional developer. Danced in the light of the sun for 13 years, before turning to the dark side. An evil MVP mentored me in the ways of C# .NET. I have not looked back since.

Interests include:

~ Windows Presentation Foundation and Silverlight
~ Parallel Programming
~ Instruction Set Simulation and Visualization
~ CPU to GPU code conversion
~ Performance Optimizations
~ Mathematics and Number Theory
~ Domain Specific Languages
~ Virtual Machine Design and Optimization
~ Graphics Development
~ Compiler Theory and Assembler Conversion Methodology
~ CUDA, OpenCL, Direct Compute, Quantum Mechanics

IEEE Associate Member 2000
This is a Organisation (No members)


Comments and Discussions