惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
D
DataBreaches.Net
B
Blog RSS Feed
D
Docker
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Y
Y Combinator Blog
A
About on SuperTechFans
V
V2EX
罗磊的独立博客
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
月光博客
月光博客
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志

博客园 - 8user

partial关键字的含义和使用 JS仿Open()打开一个新窗口,并不是弹出新窗口 判断是否含有汉字 - 8user - 博客园 时间的正则表达式验证 深入探讨IsPostBack【转】 IIS无法启动的解决方法 FCKEditor的使用及配置方法 Cool MVC: 使用正则式限定路由规则Rount - 8user C#中的值类型和引用类型 C#在线备份数据库至制定目录 - 8user - 博客园 c#实现天气预报查询 C#中的枚举器 C#里面比较时间大小三种方法 - 8user - 博客园 c#中删除ArrayList中的重复数据 - 8user - 博客园 SQL Server数据库开发的二十一条规则 c#实现程序的开机启动 - 8user - 博客园 VS2008 快捷键大全 C#调用dos命令 反射性能测试
如何统计代码行执行的时间? - 8user - 博客园
8user · 2009-06-10 · via 博客园 - 8user

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