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

  • 相关阅读:
    三年Android开发经验,挥泪整理字节跳动、微软中国凉经,你不看看吗?
    App怎么做才能永不崩溃
    做了八年的Android开发,谁不是一边崩溃,一边默默坚守!
    阿里员工年年绩效A,晒出收入后感叹:996虽然痛苦,发钱时候真香
    2021阅读书单
    不动产测绘概念
    Elasticsearch 集成
    Elasticsearch 环境
    Elasticsearch 优化
    Elasticsearch入门
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/1491808.html
Copyright © 2011-2022 走看看