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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 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 快捷键大全 - 8user 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