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

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - choice

用Adobe Flex3开发AIR应用程序–入门指南 dhtml笔记-body对象 PureMVC 相关 flash与后台数据交换方法整理4-XMLSocket篇-转载 mssql随即取数据库中的数据 Flex范例下载地址 如何更改IE查看源代码的默认打开的编辑,比如EditPlus 2、UltraEdit等 Macromedia Flex 教程: Flex入门 (1) nvarchar与varchar的区别 C#开发网站如何提高性能 - choice - 博客园 C# FAQ: const和static readonly有什么区别? Asp.Net缓存Cache使用 - choice - 博客园 asp.net程序性能优化的七个方面 (转) Flex相关 Flex2 & Flex3资料大全 FreeTextBox使用详解 (版本3.1.1) (转) GridView 几种使用方法(转) JS脚本defer的作用 - choice - 博客园 xp设置文件夹权限
C# 中 for和foreach 性能比较,提高编程性能
choice · 2007-12-15 · via 博客园 - choice

在很多情况下for和foreach具有同样的功能,选择for还是foreach很多人可能都是看自己的喜好,本测试试图通过 来真是测试数据来比较他们的执行效率。希望能给大家对他们的时候带来一些帮助。
测试环境: 
    硬件环境:   PIII800 + CPU456
    软件环境:   VisualStudio.Net  + C#
测试用例:
    利用结果集来存放记录,初始化对结果集添加记录
    分别对记录数为10000,100000,1000000条记录的时候进行采样分析
    关键测试对比代码如下,功能完全一样:
    //foreach开始时间
   datetime3 = System.DateTime.Now.TimeOfDay.ToString();
   foreach(DataRow row in relationData.Tables[RelationData.RELATIONINFO_TABLE].Rows)
 buffer = row[RelationData.PK_TABLE_NAME].ToString();
   datetime4 = System.DateTime.Now.TimeOfDay.ToString();
   //for开始时间
   datetime5 = System.DateTime.Now.TimeOfDay.ToString();
   for(int j=0;j<1000000;j++)
         buffer = relationData.Tables[RelationData.RELATIONINFO_TABLE].Rows[j][RelationData.PK_TABLE_NAME].ToString();
   datetime6 = System.DateTime.Now.TimeOfDay.ToString();
 
测试结果:
   10000条记录时:
         foreach读取时间:16:29:34.2577584
         foreach结束时间:16:29:34.2677728
         for读取开始时间:16:29:34.2677728
         for读取结束时间:16:29:34.2878016
   100000条记录时:
         foreach读取时间:16:31:10.1055808
         foreach结束时间:16:31:10.1957104
         for读取开始时间:16:31:10.1957104
         for读取结束时间:16:31:10.4460704
   1000000条记录时:
         foreach读取时间:16:33:12.6217504
         foreach结束时间:16:33:13.6332048
         for读取开始时间:16:33:13.6332048
         for读取结束时间:16:33:18.7906208
结果分析:
   1)对于10000条记录可以看出
         foreach用了 0.0100144 
         for循环用了0.0300432
         foreach所花的时间正好是for循环的 1/3
   2)对于100000条记录可以看出
         foreach用了0.0901296  
         for循环用了0.2503600
         foreach所花的时间是for循环的 36%
   3)对于1000000条记录结果可以看出
         foreach用了1.0114544  
         for循环用了4.1574160
         foreach所花的时间是for循环的 25%
    通过对这些测试结果的分析,可以看出相对于原来的for语句foreach具有
更好的执行效率,foreach的平均花费时间只有for30%。通过测试结果在for和foreach都可以使用的情况下,我 们推荐使用效率更高的foreach.在测试同时
我们附加的发现,用for写入数据时间大约是读取数据时间的10倍左右