关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx  不过里面关于安全部分的描述可以略过。

以下是我的测试代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Diagnostics; namespace ConApp_PerformanceTest
{
class Program
{
static void Main(string[] args)
{
string[] timeperiod = new string[10];
timeperiod[0] += "testArrayListWithFor ";
timeperiod[1] += "testArrayListWithForEach";
timeperiod[2] += "testArrayWithFor ";
timeperiod[3] += "testArrayWithForEach ";
timeperiod[4] += "testListWithForEach ";
timeperiod[5] += "testListWithFor ";
for (int i = 0; i < 6; i++)
{
Stopwatch sw = new Stopwatch();
TestData td = new TestData();
sw.Reset();
sw.Start();
td.testArrayListWithFor();
sw.Stop();
timeperiod[0] += " " + sw.ElapsedMilliseconds.ToString("00000"); sw.Reset();
sw.Start();
td.testArrayListWithForEach();
sw.Stop();
timeperiod[1] += " " + sw.ElapsedMilliseconds.ToString("00000"); sw.Reset();
sw.Start();
td.testArrayWithFor();
sw.Stop();
timeperiod[2] += " " + sw.ElapsedMilliseconds.ToString("00000"); sw.Reset();
sw.Start();
td.testArrayWithForEach();
sw.Stop();
timeperiod[3] += " " + sw.ElapsedMilliseconds.ToString("00000"); sw.Reset();
sw.Start();
td.testListWithForEach();
sw.Stop();
timeperiod[4] += " " + sw.ElapsedMilliseconds.ToString("00000"); sw.Reset();
sw.Start();
td.testListWithFor();
sw.Stop();
timeperiod[5] += " " + sw.ElapsedMilliseconds.ToString("00000");
}
Console.Clear();
for (int i = 0; i < 10; i++)
Console.WriteLine(timeperiod[i]);
Console.Read(); }
} class TestData
{
public TestData()
{
}
public void testArrayListWithFor()
{
ArrayList sa;
object ss;
sa = new ArrayList();
for (int i = 0; i < 10000000; i++)
sa.Add("This is a string");
for (int i = 0; i < 1000000; i++)
ss = sa[i];
} public void testArrayListWithForEach()
{
ArrayList sa;
string ss;
sa = new ArrayList();
for (int i = 0; i < 10000000; i++)
sa.Add("This is a string");
foreach (string s in sa)
ss = s;
} public void testListWithForEach()
{
List<string> sa;
string ss;
sa = new List<string>();
for (int i = 0; i < 10000000; i++)
sa.Add("This is a string");
foreach (string s in sa)
ss = s;
} public void testListWithFor()
{
List<string> sa;
string ss;
sa = new List<string>();
for (int i = 0; i < 10000000; i++)
sa.Add("This is a string");
for (int i = 0; i < 10000000; i++)
ss = sa[i];
} public void testArrayWithFor()
{
string[] sa;
string ss;
sa = new string[10000000];
for (int i = 0; i < 10000000; i++)
sa[i] = "This is a string";
for (int i = 0; i < 10000000; i++)
ss = sa[i];
} public void testArrayWithForEach()
{
string[] sa;
string ss;
sa = new string[10000000];
for (int i = 0; i < 10000000; i++)
sa[i] = "This is a string";
foreach (string s in sa)
ss = s;
}
}
}

  然后是测试结果:

所以当需要改善性能的时候,不仅需要用for,而且最好是用连续的存储空间类型的集合。

性能改善之For与Foreach的更多相关文章

  1. JFace TableViewer性能改善 -- 使用VirtualTable

    前一篇提到了SWT中的table的通过使用virtual table性能得到很大的改善,那么如果既存的工程中使用的是TableViewer来创建的表,也能改成virtual table吗? 答案是肯定 ...

  2. EF的性能改善和思考

    EF是个工具,用的好了性能就会很好,用的不好性能就会有很大损失. 先从EF的设计思想来讲解 EF的初衷是根据缓存中的实体对象,以及实体对象的状态(删除.更新.添加)来对数据库进行操作,这些实体对象.以 ...

  3. WPF性能改善---之化整为零(蜂窝的衍生应用)

    在有的项目中,有这样的需求,由于显示器的显示区域是有限的,而软件却要展示一个远大于显示区域的一些元素,此时就要引入放大.缩小.拖动等UI控制技术,而在使用这些技术的同时,在后台有效的控制渲染元素的个数 ...

  4. SWT table性能改善 -- 使用VirtualTable

    在SWT程序中使用table展示数据时,如果数据过多,执行起来会比较慢,不过,我们可以借助VirtualTable来解决这一问题. Eclipse官网中关于VirtualTable的说明见:http: ...

  5. 性能改善后复杂SQL

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

  6. 待性能改善的一个SQL

    select t.*, t.rowid from tb_tk_datasakusei_ctrl t; alter table ATOMBB.TB_TK_JISSEKI_INFO_DETAIL add ...

  7. 细数改善WPF应用程序性能的10大方法

    WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...

  8. [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能

    原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...

  9. Web标准中用于改善Web应用程序性能的各种方法总结

    提起Web应用程序中的性能改善,广大开发者们可能会想到JavaScript与DOM访问等基于各种既存技术的性能改善方法.最近,各种性能改善方法被汇总成为一个Web标准. 本文对Web标准中所包含的各种 ...

随机推荐

  1. (转)深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

      深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0 发表于2016年07月15号由52nlp 接上文<深度学习主机攒机小记>,这台GTX10 ...

  2. 在C#中使用C++编写的类

    现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类 ...

  3. vb6 枚举对象属性

    Option Explicit '引用Library TLI ' C:\WINDOWS\system32\TLBINF32.DLL ' TypeLib Information Private Sub ...

  4. pt-find 使用实例

    pt-find - Find MySQL tables and execute actions, like GNU find. 用法:pt-find [OPTION...] [DATABASE...] ...

  5. 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面?

    整理自:http://q.cnblogs.com/q/54726/ 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面? 或者说,浏览器收到的状态正常的conten ...

  6. Java 学习总结(一)

    1.     概述 1.1           dos命令行--常见的命令 l  dir : 列出当前目录下的文件以及文件夹 l  md : 创建目录 l  rd : 删除目录 l  cd : 进入指 ...

  7. 转载: Emmet:HTML/CSS代码快速编写神器

    Emmet:HTML/CSS代码快速编写神器 因为文章严禁转载,那本着做一个遵纪守法的好公民,我就不转载了,把链接放下面,方便查阅. http://www.iteye.com/news/27580

  8. js 实现ActiveXObject("Scripting.Dictionary") 功能

    /* 字典 ActiveXObject("Scripting.Dictionary") 项目中用到的ActiveXObject("Scripting.Dictionary ...

  9. 2014年第五届蓝桥杯C/C++程序设计本科B组决赛

    1.年龄巧合(枚举) 2.出栈次序(推公式/Catalan数) 3.信号匹配(kmp) 4.生物芯片(完全平方数) 5.Log大侠(线段树) 6.殖民地 1.年龄巧合 小明和他的表弟一起去看电影,有人 ...

  10. Relatives

    Description Given n, a positive integer, how many positive integers less than n are relatively prime ...