关于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. Oracle的in/not in(x,...,null)

    简述 cola in (x,y,z)等价于 cola=x or cola=y or cola=z cola not in (x,y,z)等价于 !(cola=x or cola=y or cola=z ...

  2. java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件

    /** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...

  3. linux之各个文件夹作用

    /bin 二进制可执行命令  /dev 设备特殊文件  /etc 系统管理和配置文件  /etc/rc.d 启动的配置文件和脚本  /home 用户主目录的基点,比如用户user的主目录就是/home ...

  4. MFC 线程使用

    CWinThread*  m_pthread; //多线程对象 struct param//参数传递结构体 { int id; TASK t[20]; }; CNB400Dlg::CNB400Dlg( ...

  5. 一个链式调用 setTimeout的例子

    <div> 现在时间是:<input type="text" id="name1" size="16" value=&qu ...

  6. windows端加密程序,lua代码,ZeroBrane调试

    发一个自己改的zerobrane版本(启动中文,快捷键改成和一样:F5启动调试,F9断点,F10逐过程,F11逐语句,F12跳出函数) 在zerobrane 1.0(2015.3.13)发布的基础上改 ...

  7. java设计6大设计原则

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. Maven项目

    1,创建项目,类似于创建一个VS.net 项目的 解决方案.    http://stackoverflow.com/questions/6328778/how-to-create-an-empty- ...

  9. 单例设计模式getInstance()

    对象的实例化方法,也是比较多的,最常用的方法是直接使用new,而这是最普通的,如果要考虑到其它的需要,如单实例模式,层次间调用等等.   直接使用new就不可以实现好的设计好,这时候需要使用间接使用n ...

  10. openfire

    wget http://download.igniterealtime.org/openfire/openfire-4.0.0-1.i386.rpm rpm -ivh openfire-4.0.0-1 ...