`
varsoft
  • 浏览: 2552682 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

[原创]如何改善Managed Code的Performance和Scalability系列之一:由for V.S. for each想到的

阅读更多

一直想写一系列如何提高Performance和Scalability的文章,把我的相关经验和所知道的相关的技巧同大家分享。前一阵在园子里有一篇讨论for each 和 for两种循环那个具有更好的performance的blog,议论得沸沸扬扬。我觉得这是一个很好的切入点,我就已此作为引子,开始我的这个系列的文章。这篇文章的重点不是在于比较这两种循环孰优孰劣,我将讨论的重点是如何更好地定义Collection,如何在判断在什么时候该用Array,什么时候用Collection。

一、for each的本质

我们知道,所有实现了System.Collections. IEnumerable接口的类,我们都可以对它运用for each loop。为了弄清楚它的执行过程,我先给出一个Sample:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;

namespaceArtech.CustomCollection
{
structEmployee
{
PrivateFields#regionPrivateFields
privatestring_employeeID;
privatestring_name;
privateint_age;
#endregion


Constructor#regionConstructor
publicEmployee(stringemployeeID,stringname,intage)
{
this._employeeID=employeeID;
this._name=name;
this._age=age;
}

#endregion


PublicProperties#regionPublicProperties

publicstringEmployeeID
{
get{return_employeeID;}
set{_employeeID=value;}
}


publicstringName
{
get{return_name;}
set{_name=value;}
}


publicintAge
{
get{return_age;}
set{_age=value;}
}


#endregion


Tostring#regionTostring
publicoverridestringToString()
{
returnstring.Format("\tID:\t{0}\n\tName:\t{1}\n\tAge:\t{2}\n\t",this._employeeID,this._name,this._age);
}

#endregion

}


classEmployeeList:IEnumerable
{
privateEmployee[]_employees;

publicEmployee[]Employees
{
get{return_employees;}
set{_employees=value;}
}


IEnumerableMembers#regionIEnumerableMembers

publicvirtualIEnumeratorGetEnumerator()
{
Console.WriteLine(
"EmployeeList.GetEnumerator()isinvoked");
returnnewGenericEmployeeEnumerator(this._employees);
}


#endregion

}


classEmployeeEnumerator:IEnumerator
{
privateEmployee[]_employees;
privateint_index=-1;

publicEmployeeEnumerator(Employee[]employees)
{
this._employees=employees;
}


IEnumeratorMembers#regionIEnumeratorMembers

publicobjectCurrent
{
get
{
Console.WriteLine(
"EmployeeEnumerator.Currentisinvoked");
returnthis._employees[this._index];
}

}


publicboolMoveNext()
{
Console.WriteLine(
"EmployeeEnumerator.MoveNextisinvoked");

if(this._index<this._employees.Length-1)
{
this._index++;
returntrue;
}

returnfalse;
}


publicvoidReset()
{
this._index=-1;
}

#endregion

}

classProgram
{
staticvoidMain(string[]args)
{
Employee[]employees
=newEmployee[]{newEmployee("0001","ZhangSan",21),newEmployee("0002","LiSi",30)};
EmployeeListempoyeeList
=newEmployeeList();
empoyeeList.Employees
=employees;

Console.WriteLine(
"Beginforeachloop");
foreach(EmployeeemployeeinempoyeeList)
{
Console.WriteLine(employee.ToString());
}

Console.WriteLine(
"\n\nBeginwhileloop");
IEnumeratorenumerator
=empoyeeList.GetEnumerator();
while(enumerator.MoveNext())
{
Employeeemployee
=(Employee)enumerator.Current;
Console.WriteLine(employee.ToString());
}

}

}

}

我们先运行一下上面的程序再来讲述具体的执行的流程。


在上面的Sample中我们先定义了一个Employee的struct,之所以使用struct而不用一般的class,我将在后面的部分介绍。

structEmployee
{
PrivateFields#regionPrivateFields
privatestring_employeeID;
privatestring_name;
privateint_age;
#endregion


Constructor#regionConstructor
publicEmployee(stringemployeeID,stringname,intage)
{
this._employeeID=employeeID;
this._name=name;
this._age=age;
}

#endregion


PublicProperties#regionPublicProperties

publicstringEmployeeID
{
get{return_employeeID;}
set{_employeeID=value;}
}


publicstringName
{
get{return_name;}
set{_name=value;}
}


publicintAge
{
get{return_age;}
set{_age=value;}
}


#endregion


Tostring#regionTostring
publicoverridestringToString()
{
returnstring.Format("\tID:\t{0}\n\tName:\t{1}\n\tAge:\t{2}\n\t",this._employeeID,this._name,this._age);
}

#endregion

}

然后我们基于这个Emplyee struct,定义了与之对应的Collection:EmplyeeList。EmplyeeList实现了System.Collections. IEnumerable接口。并以一个virtual 方法的形式实现了该接口的GetEnumerator方法。(为什么用virtual方法的原因,我会再后续部分解释)。

PublicvirtualIEnumeratorGetEnumerator()
<
分享到:
评论

相关推荐

    okhttp-3.14.9-API文档-中英对照版.zip

    赠送jar包:okhttp-3.14.9.jar; 赠送原API文档:okhttp-3.14.9-javadoc.jar; 赠送源代码:okhttp-3.14...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    DirectX for Managed Code

    DirectX for Managed Code是微软为.NET开发者提供的一套API,旨在让C#和其他.NET语言的程序员能够充分利用DirectX的功能来创建高性能的图形和多媒体应用程序。DirectX是一个包含多个子组件的集合,主要用于游戏开发...

    Professional C#4.0 and .NET 4.0 part2

    Part V: Presentation Chapter 35: Core WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .983 Chapter 36: Business Applications with WPF . . . . . . . . . . . . . . . . . . . .1035 ...

    Professional C# 4.0 and .NET 4.0 part1

    Part V: Presentation Chapter 35: Core WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .983 Chapter 36: Business Applications with WPF . . . . . . . . . . . . . . . . . . . .1035 ...

    ORACLE-DataGuard系列:逻辑standby搭建.doc

    ### ORACLE DataGuard系列:逻辑Standby搭建 #### 概述 Oracle Data Guard是一种全面的解决方案,用于提供最高级别的数据保护、数据可用性和灾难恢复。它通过在远程位置创建并维护一个或多个完整副本(Standby...

    WebLogic错误 java.lang.AssertionError: Registered more than one

    这个错误表明有多个相同的MBean(Managed Beans)实例注册到JMX(Java Management Extensions)服务器上,导致管理对象名称冲突。 **解决方案** 针对此问题,Oracle提供了相应的补丁来修复。补丁号与WebLogic ...

    PostgreSQL JDBCL连接:postgresql-42.2.5.jar包.zip

    这个jar包包含了一系列的类和接口,用于执行SQL语句、处理结果集、管理事务等操作。 **2. 添加依赖** 在使用`postgresql-42.2.5.jar`之前,需要将其添加到项目的类路径中。对于传统的Java项目,可以将jar文件放入`...

    EJB3.rar_site:www.pudn.com

    **EJB(Enterprise JavaBean)** 是Java Enterprise Edition(JavaEE)的重要组成部分,它提供了一种规范化的框架来开发和部署企业级的分布式应用程序。EJB标准由Java Community Process(JCP)制定,旨在简化服务器...

    高中英语必修1人教版精品导学案:Unit 4 Earthquakes.doc

    - 损害,损害 v.: damage - 幸存者,残存物:survivor - 毁坏 v.: devastate - 砖块:brick - 无用的:useless - 震撼,攻击:jolt - 营救:rescue - 电:electricity - 灾害:disaster - 结构 v.: ...

    最新Color Admin1.9 - bootstrap3响应后台模板+前端网页模板

    Managed Table 9 DataTables Plugins DataTables Extension Combination Flot Charts Morris Charts Chart JS Chart d3 Calendar Maps Gallery Gallery v2 Blank Page Page With Footer Page Without ...

    dsTest.war.zip_site:www.pudn.com

    【标题】"dsTest.war.zip_site:www.pudn.com" 暗示这是一个包含Web应用程序资源的压缩文件,dsTest.war是Java Web应用的标准打包格式,它是由Java Servlet技术规范定义的。war(Web Archive)文件是将所有Web应用...

    EurekaLog_7.5.0.0_Enterprise

    16)..Added: Support for environment variables in project option's paths 17)..Added: Support for relative file paths and environment variables for events and various module paths 18)..Added: Logging ...

    Managed.Code.Rootkits.2011

    《Managed.Code.Rootkits.2011》是关于.NET Rootkits的一个专题,深入探讨了在.NET框架下创建和检测Rootkit的技术。Rootkit是一种恶意软件工具,它隐藏在操作系统内部,使得攻击者能够秘密地控制受感染的系统,同时...

    Chapter 5 - Improving Managed Code Performance.pdf

    在.NET平台上,托管代码性能优化是一个重要课题,微软官方文档提供了一系列的设计和编程技巧来帮助开发者提升其.NET应用程序的性能。本章的目的是提供一系列优化和改进托管代码性能的技术,指导开发者编写可以充分...

    Quest.Toad.for.SQL.Server.v3.0.0.736

    Quest Toad for SQL Server是一款由Quest Software开发的专业数据库管理和开发工具,主要用于Microsoft SQL Server环境。在SQL Server管理员和开发人员的日常工作中,它扮演着至关重要的角色,提供了一整套功能强大...

    Kali Linux VMware(kali-linux-2022.2-vmware-i386.part1.rar)

    Kali Linux VMware(kali-linux-2022.2-vmware-i386.7z)文件分割成 3个 压缩包,必须集齐3个 文件后才能一起解压一起使用: Kali Linux VMware(kali-linux-2022.2-vmware-i386.part1.rar) ... ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Dundas Chart ASP.NET Enterprise Edition is a fully managed, CLR (Common Language Runtime) compliant charting component designed for ASP.NET development. Included is support for all standard and many ...

    DirectX for Manager Code

    总的来说,"DirectX for Managed Code"是一个让.NET开发者能够轻松利用DirectX功能的工具,尽管它在较新的开发环境中可能不是首选,但对于需要在.NET环境中利用DirectX特性的项目,它仍然是一个宝贵的资源。

    WindowsMobile 6.5滑动效果

    http://code.msdn.microsoft.com/gestureswm http://msdn.microsoft.com/zh-cn/windowsmobile/default.aspx [Windows Mobile]Gestures API 擷取使用者的手勢 :...

    Software Testing and Continuous Quality Improvement

    Plan for Implementation of the SQA Plan . . . . . . . . . . . . . 19 Step 5. Execute the SQA Plan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Quality Standards. . . . . . . . ...

Global site tag (gtag.js) - Google Analytics