性能改善之For与Foreach
关于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的更多相关文章
- JFace TableViewer性能改善 -- 使用VirtualTable
前一篇提到了SWT中的table的通过使用virtual table性能得到很大的改善,那么如果既存的工程中使用的是TableViewer来创建的表,也能改成virtual table吗? 答案是肯定 ...
- EF的性能改善和思考
EF是个工具,用的好了性能就会很好,用的不好性能就会有很大损失. 先从EF的设计思想来讲解 EF的初衷是根据缓存中的实体对象,以及实体对象的状态(删除.更新.添加)来对数据库进行操作,这些实体对象.以 ...
- WPF性能改善---之化整为零(蜂窝的衍生应用)
在有的项目中,有这样的需求,由于显示器的显示区域是有限的,而软件却要展示一个远大于显示区域的一些元素,此时就要引入放大.缩小.拖动等UI控制技术,而在使用这些技术的同时,在后台有效的控制渲染元素的个数 ...
- SWT table性能改善 -- 使用VirtualTable
在SWT程序中使用table展示数据时,如果数据过多,执行起来会比较慢,不过,我们可以借助VirtualTable来解决这一问题. Eclipse官网中关于VirtualTable的说明见:http: ...
- 性能改善后复杂SQL
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...
- 待性能改善的一个SQL
select t.*, t.rowid from tb_tk_datasakusei_ctrl t; alter table ATOMBB.TB_TK_JISSEKI_INFO_DETAIL add ...
- 细数改善WPF应用程序性能的10大方法
WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...
- [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能
原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...
- Web标准中用于改善Web应用程序性能的各种方法总结
提起Web应用程序中的性能改善,广大开发者们可能会想到JavaScript与DOM访问等基于各种既存技术的性能改善方法.最近,各种性能改善方法被汇总成为一个Web标准. 本文对Web标准中所包含的各种 ...
随机推荐
- dpkg ---- apt-get ------ aptitude 三种方式的区别 及命令格式
转自:http://blog.csdn.net/xiaoyanghuaban/article/details/22946987 dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的 ...
- mm/makefile
## Makefile for the linux memory manager.## Note! Dependencies are done automagically by 'make dep', ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Sprint第一个冲刺(第九天)
一.Sprint介绍 建立云端数据库,把注册的内容保存到云端,不易丢失. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:
- pclint vc6/linux 工程,测试正常
下载:http://www.gimpel.compojie版搜索一下很好找 里面有vc6 和linux工程的lnt文件,实际使用可能要修改. http://files.cnblogs.com/file ...
- Squid
事件:由于我们在运维过程中需要升级或安装新的开源软件或组件时,相关的依赖包或基础包非常非常多. 因安全限制,对于没有访问internet权限的服务器,在执行安装或升级过程中就非常容易出错. 所以我们需 ...
- Android控件之SlidingDrawer(滑动式抽屉)详解与实例
SlidingDrawer效果想必大家也见到过,它就是1.5模拟器上进入应用程序列表的效果.下面是截图 一.简介 SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容.它 ...
- 网站tomcat配置
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF ...
- 每日学习心得:SharePoint 2013 自定义列表项添加Callout菜单项、文档关注、SharePoint服务端对象模型查询
前言: 前一段时间一直都比较忙,没有什么时间进行总结,刚好节前项目上线,同时趁着放假可以好好的对之前遇到的一些问题进行总结.主要内容有使用SharePoint服务端对象模型进行查询.为SharePoi ...
- Android模拟器部署历程
由于想玩一款手机的游戏,本人手机Android系统版本太低,不能安装.所以就想在WindowS上安装一个模拟器,然后安装游戏.想法挺好.实现起来确实经历了一个坎坷的过程.为了让其他人少走弯路,本人把此 ...