zoukankan      html  css  js  c++  java
  • 如何统计代码行执行的时间?

    1.         问题

    请计算下列代码执行所耗费的时间,给出通用的解决方法?

    List<Int32> myList=new List<Int32>();

              for(Int32 i=0;i<ICount;i++)

              {

                                        

              myList.Add(i);

              }

    2.         解决方法

     

    using (OperationTimer op=new OperationTimer ("Generic "))

    {

                                List<Int32> myList=new List<Int32>();

                                for(Int32 i=0;i<ICount;i++)

                                {

                                        

                                         myList.Add(i);

                                }

                                }

    }

    OperationTimer 类会在调用构造函数时取得时间t1,在调用Dispose()取得时间t2,中间过程耗费的时间=t2-t1. Using 语法会在using 块的末尾调用using中声明对象的Dispose方法。

                      

    3.         代码

    using System;

    using System.Diagnostics;

    using System.Collections;

    using System.Collections.Generic;

    namespace genericApp

    {

       

        ///计时辅助类

             public class OperationTimer:IDisposable{

                      

                       private Int64 m_startTime;

                       private string m_text;

                       private Int32 m_CollectionCount;

                      

                       public OperationTimer(string text)

                       {

                                m_startTime=Stopwatch.GetTimestamp();

                                m_text=text;

                               

                       }

                      

                       public void Dispose()

                       {

                                string text=string.Format("Time={0,6:###.00}",(Stopwatch.GetTimestamp()-m_startTime)/Stopwatch.Frequency);

                                Console.WriteLine(string.Format("{0}:{1}",m_text,text));

                       }

                      

                      

             }

            

             class MainClass

             {

                       public static void Main(string[] args)

                       {

                                ValueTypePerfermanceTest();

                                Console.Read();

                       }

                      

                       static void ValueTypePerfermanceTest()

                       {

                                const Int32 ICount=20000000;

                               

                                using (OperationTimer op=new OperationTimer ("Generic "))

                                {

                                         List<Int32> myList=new List<Int32>();

                                for(Int32 i=0;i<ICount;i++)

                                {

                                        

                                         myList.Add(i);

                                }

                                }

                                using (OperationTimer op2=new OperationTimer ("ArrayList "))

                                {

                                         ArrayList myList=new ArrayList();

                                for(Int32 i=0;i<ICount;i++)

                                {

                                        

                                         myList.Add(i);

                                }

                                }

                               

                               

                       }

             }

            

            

    }

    4.         Mono 2.4 中的运行结果

    Generic.jpg

  • 相关阅读:
    asp.net using library ClosedXML to export excel
    javascript 属性的特性
    javascript 如何避免属性访问错误
    javascript 继承
    Chapter 4 : Character Strings and Formatted Input/Output
    Chapter 3 :Differentiation
    Chapter 3 : Data and C
    Chapter 3 : Data and C
    Chapter 4 : Strings and Formatted Input/Output
    Chapter 1 :Preliminaries
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/1491808.html
Copyright © 2011-2022 走看看