教你50招提升ASP.NET性能(二十):认识你的循环
(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性能(二十):认识你的循环的更多相关文章
- 教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员
(27)Throw hardware at the problem, not developers 招数27: 把问题仍给硬件而不是开发人员 As developers, we often want ...
- 教你50招提升ASP.NET性能(十九):静态集合
(30)Static collections 招数30: 静态集合 If a collection is static, make sure it only contains the objects ...
- 教你50招提升ASP.NET性能(十八):在处理网站性能问题前,首先验证问题是否出在客户端
(29)Before tackling any website performance issue, first verify the problem isn’t on the client 招数29 ...
- 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
(26)Don’t underestimate the value of the UI when tackling performance problems 招数26: 解决性能问题时不要低估UI的价 ...
- 教你50招提升ASP.NET性能(十四):使用startMode属性来减少ASP.NET站点加载时间
(25)Use the startMode attribute to reduce the load time for your ASP.NET site 招数25: 使用startMode属性来减少 ...
- 教你50招提升ASP.NET性能(十):减少通过网络发送的数据
(16)Reduce the data sent across the network 招数16: 减少通过网络发送的数据 Reducing the amount of data sent acros ...
- 教你50招提升ASP.NET性能(十二):在生产环境,仔细考虑你需要记录哪些日志
(18)When in production, carefully consider what you need to log 招数18: 在生产环境,仔细考虑你需要记录哪些日志 Many peopl ...
- 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧
Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...
- 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站
(17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...
- 教你50招提升ASP.NET性能(七):总是在服务器端执行验证
(13)Always perform validation on the server as well 招数13: 总是在服务器端执行验证 This isn’t exactly a performan ...
随机推荐
- CURL使用2
一:LibCurl 编程流程1.调用curl_global_init()初始化libcurl2.调用 curl_easy_init()函数得到 easy interface型指针3.调用curl_ea ...
- LT1619EMS8 锂电池 升压电路分析
LT1619EMS8 锂电池 升压电路分析 本文主要是分析LT1619EMSB锂电池升压芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. 2016-1-23 深圳 南山平山村 曾剑锋 一. ...
- 【CSS】使用CSS改变超链接样式
超链接代码 <ahrefahref="http://www.divCSS5.com/"target="_blank" title="关于divC ...
- Oracle 存储过程的创建,及触发器调用存储过程
一.创建存储过程 1.存储过程写法 create or replace procedure HVM_BYQ_TJ --变压器统计信息--->入库 (id in number) as begin ...
- How to Calculate difference between two dates in C# z
Do you need to find the difference in number of days, hours or even minute between the two date rang ...
- 1、ListView自定义控件下拉刷新(一)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo ...
- Delphi TRichEdit加载word内容
procedure TForm1.btn6Click(Sender: TObject);var WordApp: Variant; //声明一个word对象beginWordApp := Create ...
- HDU5790 Prefix 字典树+主席树
分析:这个题和spoj的d_query是一个题,那个是求一段区间里有多少个不同的数字,这里是统计有多少个不同的前缀 用字典树进行判重,(和查询不同的数字一样)对于每个不同的前缀,只保留它最后一次出现的 ...
- 【LeetCode 207】Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Android FragmentActivity+viewpager的使用
使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能. package com.lanyuweng.mibaby; import android.content.Intent; impo ...