(31)Know your loops

招数31:

认识你的循环

for is the fastest way of iterating over a collection, foreach is a little slower, and LINQ queries are slowest.
for是遍历集合最快的方法,foreach略慢一些,LINQ查询最慢。

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics; namespace ConsoleApplicationExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("code test:"); Stopwatch watch = new Stopwatch();
// for loop
watch.Start();
for (int i = ; i < ; i++) { ; }
watch.Stop();
Console.WriteLine("for loop:");
Console.WriteLine(watch.Elapsed.TotalMilliseconds); watch = new Stopwatch();
// while loop
watch.Start();
int loop = ;
while (loop < ) { loop++; }
watch.Stop();
Console.WriteLine("while loop:");
Console.WriteLine(watch.Elapsed.TotalMilliseconds); watch = new Stopwatch();
// foreach loop
watch.Start();
int[] array = new int[];
foreach (int i in array) { ; }
watch.Stop();
Console.WriteLine("foreach loop:");
Console.WriteLine(watch.Elapsed.TotalMilliseconds); watch = new Stopwatch();
// foreach loop
watch.Start();
Array.ForEach(array, (i) => { ; });
watch.Stop();
Console.WriteLine("lamda loop:");
Console.WriteLine(watch.Elapsed.TotalMilliseconds); Console.ReadLine();
}
}
}

测试结果:

code test:
for loop:
0.2467
while loop:
0.2666
foreach loop:
0.4867
lamda loop:
0.8728

教你50招提升ASP.NET性能(二十):认识你的循环的更多相关文章

  1. 教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员

    (27)Throw hardware at the problem, not developers 招数27: 把问题仍给硬件而不是开发人员 As developers, we often want ...

  2. 教你50招提升ASP.NET性能(十九):静态集合

    (30)Static collections 招数30: 静态集合 If a collection is static, make sure it only contains the objects ...

  3. 教你50招提升ASP.NET性能(十八):在处理网站性能问题前,首先验证问题是否出在客户端

    (29)Before tackling any website performance issue, first verify the problem isn’t on the client 招数29 ...

  4. 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值

    (26)Don’t underestimate the value of the UI when tackling performance problems 招数26: 解决性能问题时不要低估UI的价 ...

  5. 教你50招提升ASP.NET性能(十四):使用startMode属性来减少ASP.NET站点加载时间

    (25)Use the startMode attribute to reduce the load time for your ASP.NET site 招数25: 使用startMode属性来减少 ...

  6. 教你50招提升ASP.NET性能(十):减少通过网络发送的数据

    (16)Reduce the data sent across the network 招数16: 减少通过网络发送的数据 Reducing the amount of data sent acros ...

  7. 教你50招提升ASP.NET性能(十二):在生产环境,仔细考虑你需要记录哪些日志

    (18)When in production, carefully consider what you need to log 招数18: 在生产环境,仔细考虑你需要记录哪些日志 Many peopl ...

  8. 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧

    Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...

  9. 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站

    (17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...

  10. 教你50招提升ASP.NET性能(七):总是在服务器端执行验证

    (13)Always perform validation on the server as well 招数13: 总是在服务器端执行验证 This isn’t exactly a performan ...

随机推荐

  1. pip

    查看安装的包 pip list

  2. 在Windows下编译FFmpeg详细说明

    MinGW:一个可自由使用和自由发布的Windows特定头文件和使用GNC工具集导入库的集合,允许你生成本地的Windows程序而不需要第三方C运行时 MinGW,即 Minimalist GNU F ...

  3. 【DFS+堆的二叉树结构】15轻院校赛-J-堆

    [题目链接:J-堆] 1734: 堆 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 239  Solved: 113 SubmitStatusWeb B ...

  4. TCP/IP详解学习笔记(12)-TCP的超时与重传

    超时重传是TCP协议保证数据可靠性的另一个重要机制,其原理是在发送某一个数据以后就开启一个计时器,在一定时间内如果没有得到发送的数据报的ACK报文,那么就重新发送数据,直到发送成功为止. 1.超时 超 ...

  5. Android学习笔记一

    一.eclipse中的十大快捷键: 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的 ...

  6. Launcher2编译

    Android的源码包,压缩文件大概有3个G左右,要使用其中自带的一些源码需要很多技巧,否则会提示找不到一些库,大量的报错让人心神不定,不知所从. 我拿桌面代码举个例子吧. 桌面代码在源码包的pack ...

  7. codeforces 682D Alyona and Strings

    #include <cstdio> #include <iostream> #include <ctime> #include <vector> #in ...

  8. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  9. Ext列表展现--普通排序sortable--全局排序remoteSort(EXTJS 全局排序问题)

    关于Ext的排序问题,一般涉及到两种方式. A.一种是默认的客户端排序机制,对当前页进行排序.sortable 这种排序模式不用多说,是人都会: 1.可以在Ext.grid.ColumnModel列模 ...

  10. 制作java可执行程序的方法

    命令行方法: 1. 创建Manifest.txt文件,内容: Main-Class: com.mkyong.awt.AwtExample 2.打包所有的class,包括Manifest.txt文件: ...