性能改善之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标准中所包含的各种 ...
随机推荐
- mysql workbench is well-designed
我用过mysql纯cli(command line),即纯命令行的执行环境,也用过navicat for mysql pro 破解版,甚至还用过比较高端的sqlyog,最后我还是选择了官方的workb ...
- vs2010编译出的exe“应用程序无法正常启动(0xc0150002)”
今天编译出一个使用ogre1.6.5动态库的应用程序,启动时报"应用程序无法正常启动(0xc0150002)"的错误提示. 编译环境是Win10+VS2010.这个错误可以在Win ...
- hessian不能注入dao的问题解决
天天卡,写个程序很费劲,不是卡这儿就是卡那里,一天天的不出活,周六日费时间都在这上面了. 问题:hessian能调通,就是不能操作数据库,userDao不能注入,为null,期初以为是实体类赋值的问题 ...
- linux 下 文件权限和文件主
文件与文件夹的权限和所有者 1.chmod -R 755 file 777 含义与来源: 777含义:分别为:所有者.同组用户.其他用户 7的来源:文件有三种操作模式:读4.写2.执行1,分别值为42 ...
- (转)AVI文件格式解析+AVI文件解析工具
AVI文件解析工具下载地址:http://download.csdn.net/detail/zjq634359531/7556659 AVI(Audio Video Interleaved的缩写)是一 ...
- C# 文件大小
/// <summary> /// 获取文件大小 /// </summary> /// <param name="sFullName">< ...
- SourceInsight 精确导入Linux kernel源码的方法
相信有很多人用 SourceInsight 查看 Linux Kernel 源码,但导入源码时会遇到一些问题.1.如果把整个源码树都导入进去,查看符号定义的时候,会发现有大量重复定义,很难找到正确的位 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- 自动化测试selenium+java 第三章
import java.sql.Time;import java.util.concurrent.TimeUnit; import org.junit.BeforeClass;import org.o ...
- iOS开发编译报错、常见问题(实时更新)
一.报错与警报 1.错误代码:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCH ...