コード例 #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        var result = new List<ForLoopResourcesResult>();

        var theData = new List<string>();
        for (int i = 0; i < 9999999; i++)
            theData.Add("Item" + i);

        int MaxLoop = 5;

        var S1 = new ForLoopResourcesResult() { Description = "Storing the count in a variable", Duration = 0, MemoryUsage=0 };
        result.Add(S1);
        for (int Loop = 0; Loop < MaxLoop; Loop++)
        {
            using (var tOp = new TimeOperation())
            {
                long MemoryStart = System.GC.GetTotalMemory(false);
                var Count = theData.Count;
                for (int i = 0; i < Count; i++)
                {
                    string s = theData[i];
                }
                S1.Duration += tOp.Stop();
                long MemoryEnd = System.GC.GetTotalMemory(false);
                S1.MemoryUsage += MemoryEnd - MemoryStart;
            }
        }
        System.Threading.Thread.Sleep(5000);
        System.GC.Collect();
        System.Threading.Thread.Sleep(5000);

        var S2 = new ForLoopResourcesResult() { Description = "Normal For Loop", Duration = 0, MemoryUsage=0 };
        result.Add(S2);
        for (int Loop = 0; Loop < MaxLoop; Loop++)
        {
            using (var tOp = new TimeOperation())
            {
                long MemoryStart = System.GC.GetTotalMemory(false);
                for (int i = 0; i < theData.Count; i++)
                {
                    string s = theData[i];
                }
                S2.Duration += tOp.Stop();
                long MemoryEnd = System.GC.GetTotalMemory(false);
                S1.MemoryUsage += MemoryEnd - MemoryStart;

            }
        }
        System.Threading.Thread.Sleep(5000);
        System.GC.Collect();
        System.Threading.Thread.Sleep(5000);

        var S3 = new ForLoopResourcesResult() { Description = "Using List.ForEach", Duration = 0, MemoryUsage=0 };
        result.Add(S3);
        for (int Loop = 0; Loop < MaxLoop; Loop++)
        {
            using (var tOp = new TimeOperation())
            {
                long MemoryStart = System.GC.GetTotalMemory(false);
                theData.ForEach(delegate(string str) { string s = str; });
                long MemoryEnd = System.GC.GetTotalMemory(false);
                S1.MemoryUsage += MemoryEnd - MemoryStart;

                S3.Duration += tOp.Stop();
            }
        }
        System.Threading.Thread.Sleep(5000);
        System.GC.Collect();
        result.ForEach(delegate(ForLoopResourcesResult obj) { obj.Duration = obj.Duration / MaxLoop; });
        GV.DataSource = result;
        GV.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        var oArrayList = new ArrayList();
        var oList = new List<int>();
        var oHashTable = new Hashtable();
        var oDictionary = new Dictionary<int, int>();

        var results = new List<ComparisonObj>();

        /*
         * Create a new Comparison Object
         * a Comparison object stores the various parameters which we want to monitor
         */

        var coArrayList = new ComparisonObj() { Description = "ArrayList"};
        results.Add(coArrayList);

        //ArrayList
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            oArrayList = new ArrayList();

            //Insert items to the array List
            using (var tOps = new TimeOperation())
            {
                for (int i = 0; i < MaxElements; i++)
                    oArrayList.Add(i);
                coArrayList.Duration_Insert += tOps.Stop();
            }

            //Iterate thru all items in the array list
            using (var tOps = new TimeOperation())
            {
                foreach (object o in oArrayList)
                {
                    int i = (int)o;
                }
                coArrayList.Duration_Iterate += tOps.Stop();
            }

            //Seek the middle number in the arraylist
            using (var tOps = new TimeOperation())
            {
                oArrayList.Contains(MaxElements / 2);
                coArrayList.Duration_Search += tOps.Stop();
            }

            oArrayList.Clear();
        }

        System.Threading.Thread.Sleep(5000);
        var coList = new ComparisonObj() { Description = "List"};
        results.Add(coList);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            oList = new List<int>();

            using (var tOps = new TimeOperation())
            {
                for (int i = 0; i < MaxElements; i++)
                    oList.Add(i);
                coList.Duration_Insert += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                foreach (var o in oList)
                {
                    int i = o;
                }
                coList.Duration_Iterate += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                oList.Contains(MaxElements / 2);
                coList.Duration_Search += tOps.Stop();
            }

            oList.Clear();
        }
        System.Threading.Thread.Sleep(5000);
        var coHashTable = new ComparisonObj() { Description = "HashTable" };
        results.Add(coHashTable);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            oHashTable = new Hashtable();

            using (var tOps = new TimeOperation())
            {
                for (int i = 0; i < MaxElements; i++)
                    oHashTable.Add(i, i);
                coHashTable.Duration_Insert += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                foreach (object o in oHashTable.Keys)
                {
                    int i = (int)oHashTable[o];
                }
                coHashTable.Duration_Iterate += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                oHashTable.ContainsKey(MaxElements / 2);
                coHashTable.Duration_Search += tOps.Stop();
            }

            oHashTable.Clear();
        }

        System.Threading.Thread.Sleep(5000);
        var coDictionary = new ComparisonObj() { Description = "Dictionary" };
        results.Add(coDictionary);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            oDictionary = new Dictionary<int, int>();

            using (var tOps = new TimeOperation())
            {
                for (int i = 0; i < MaxElements; i++)
                    oDictionary.Add(i, i);
                coDictionary.Duration_Insert += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                foreach (var o in oDictionary.Keys)
                {
                    int i = (int)oDictionary[o];
                }
                coDictionary.Duration_Iterate += tOps.Stop();
            }

            using (var tOps = new TimeOperation())
            {
                oDictionary.ContainsKey(MaxElements / 2);
                coDictionary.Duration_Search += tOps.Stop();
            }

            oHashTable.Clear();
        }

        System.Threading.Thread.Sleep(5000);
        foreach (var o in results)
        {
            o.Duration_Insert = o.Duration_Insert / MaxLoops;
            o.Duration_Iterate = o.Duration_Iterate / MaxLoops;
            o.Duration_Search = o.Duration_Search / MaxLoops;
        }

        GV.DataSource = results;
        GV.DataBind();
    }
コード例 #3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        var results = new List<StringBuilderResults>();
        int MaxLoops = 5;
        int MaxIterations = 10000;

        var r1 = new StringBuilderResults() { Description = "StringBuilder", MemoryUsage = 0, Duration = 0, TotalExceptions = 0 };
        results.Add(r1);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            long memoryStart = System.GC.GetTotalMemory(false);
            using (var tOp = new TimeOperation())
            {
                try
                {
                    var sb = new StringBuilder();
                    for (int i = 0; i < MaxIterations; i++)
                    {
                        sb.Append(i.ToString());
                    }
                }
                catch (Exception ex)
                {
                    r1.TotalExceptions++;
                    r1.AddException(ex);
                }
                r1.Duration += tOp.Stop();

            }
            long memoryEnd = System.GC.GetTotalMemory(false);
            r1.MemoryUsage += memoryEnd - memoryStart;
        }
        System.Threading.Thread.Sleep(5000);
        System.GC.Collect();
        System.Threading.Thread.Sleep(5000);

        var r2 = new StringBuilderResults() { Description = "String Concat", MemoryUsage = 0, Duration = 0, TotalExceptions = 0 };
        results.Add(r2);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            long memoryStart = System.GC.GetTotalMemory(false);
            using (var tOp = new TimeOperation())
            {
                try
                {
                    var sb = "";
                    for (int i = 0; i < MaxIterations; i++)
                    {
                        sb = sb + i.ToString();
                    }
                }
                catch (Exception ex)
                {
                    r2.TotalExceptions++;
                    r2.AddException(ex);
                }
                r2.Duration += tOp.Stop();

            }
            long memoryEnd = System.GC.GetTotalMemory(false);
            r2.MemoryUsage += memoryEnd - memoryStart;
        }
        System.Threading.Thread.Sleep(5000);
        System.GC.Collect();

        results.ForEach(delegate(StringBuilderResults r) { r.Duration = r.Duration / MaxLoops; r.MemoryUsage = r.MemoryUsage / MaxLoops; });

        GV.DataSource = results;
        GV.DataBind();
    }
コード例 #4
0
    protected void Page_Load(object sender, EventArgs e)
    {
        var results = new List<DisposingResourcesResult>();
        var yDispose = new DisposingResourcesResult() { Description = "Disposing objects", MemoryUsage=0, Duration=0, TotalExceptions=0 };
        results.Add(yDispose);

        var cmd = new SqlCommand();
        cmd.CommandText = "select * from Address";
        int MaxLoops = 5;
        int MaxIterations = 100;
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            long memoryStart = System.GC.GetTotalMemory(false);
            using (var tOp = new TimeOperation())
            {
                try
                {
                    for (int i = 0; i < MaxIterations; i++)
                    {
                        using (var db = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString))
                        {
                            db.Open();
                            cmd.Connection = db;
                            using (var r = cmd.ExecuteReader())
                            {
                                while (r.Read())
                                {
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    yDispose.TotalExceptions++;
                    yDispose.AddException(ex);
                }
                    yDispose.Duration += tOp.Stop();

            }
            long memoryEnd = System.GC.GetTotalMemory(false);
            yDispose.MemoryUsage += memoryEnd - memoryStart;
        }
        System.GC.Collect();

        var nDispose = new DisposingResourcesResult() { Description = "NOT Disposing objects", MemoryUsage = 0, Duration = 0, TotalExceptions = 0 };
        results.Add(nDispose);
        for (int Loop = 0; Loop < MaxLoops; Loop++)
        {
            long memoryStart = System.GC.GetTotalMemory(false);
            using (var tOp = new TimeOperation())
            {
                for (int i = 0; i < MaxIterations; i++)
                {
                    try
                    {
                        var db = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString);
                        db.Open();
                        cmd.Connection = db;
                        using (var r = cmd.ExecuteReader())
                        {
                            while (r.Read())
                            {
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        nDispose.TotalExceptions++;
                        nDispose.AddException(ex);
                    }
                }
                nDispose.Duration += tOp.Stop();
            }
            long memoryEnd = System.GC.GetTotalMemory(false);
            nDispose.MemoryUsage += memoryEnd - memoryStart;
        }
        System.GC.Collect();

        results.ForEach(delegate(DisposingResourcesResult r) { r.Duration = r.Duration / MaxLoops; r.MemoryUsage = r.MemoryUsage / MaxLoops; });

        GV.DataSource = results;
        GV.DataBind();
    }