泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable
再使用泛型的时候,经常需要用到遍历功能:
只要继承了 TEnumerator 或 TEnumerable 这两个抽象类的 都具有遍历功能。
当然没有继承这两个抽象类的 也具有使用 for in 来遍历的功能,编译器内置的,具体可以参见万一的博客:
http://www.cnblogs.com/del/archive/2008/11/12/1332011.html

举例:

unit Unit5; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections,
Vcl.StdCtrls; type
TForm5 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; /// <summary>
/// 定义一个结构体
/// </summary>
RPerson = record
name: string;
age: Integer;
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button1Click(Sender: TObject);
var
mykey,myValue: string;
MyDic: TDictionary<string, string>;
MyDic2: TDictionary<string, RPerson>;
I: Integer;
person: RPerson;
begin
MyDic := TDictionary<string, string>.Create();
MyDic2 := TDictionary<string, RPerson>.Create();
try
//初始化
Memo1.Lines.Clear;
MyDic.Add('XiaoLi', '李飞刀');
MyDic.Add('XiaoWang', '王中王');
MyDic.Add('XiaoZhang', '张飞'); person.name := '小李飞刀';
person.age := ;
MyDic2.Add('XiaoLi', person);
person.name := '火云邪神';
person.age := ;
MyDic2.Add('XiaoHuo', person); //通过key来遍历
for mykey in MyDic.Keys do
begin
Memo1.Lines.Add(mykey);
end;
Memo1.Lines.Add(''); //通过value来遍历
for myValue in MyDic.Values do
begin
Memo1.Lines.Add(myValue);
end;
Memo1.Lines.Add(''); //通过结构体的值来遍历
for person in MyDic2.Values do
begin
Memo1.Lines.Add(person.name);
end;
finally
MyDic.Free;
MyDic2.Free;
end;
end; end.
可见 遍历 的思想不能 仅仅局限于传统的 for i = 0 to list.cout -1 这种方法,而是应该多用 for in ,for in 可以遍历 一切, 传统的 for 循环 for in 都能实现。
遍历 可以 直接遍历 一切(基本类型、结构体、动态数组、类对象) 既然这样,那么问题 又来了 谁的效率高呢,我们来PK下。

unit Unit5; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections; type
TForm5 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button1Click(Sender: TObject);
var
MyList,tempList1,tempList2: TStringList;
I: Integer;
start_time: Int64;
tempStr: string;
begin
MyList := TStringList.Create;
tempList1 := TStringList.Create;
tempList2 := TStringList.Create;
try
//先加进数据来
for I := to do
begin
MyList.Add(I.ToString);
end; start_time := GetTickCount;
for I := to MyList.Count - do
begin
//只有使用才能看到效果
tempList1.Add(MyList[I]);
end;
Memo1.Lines.Add('fot to 耗时:' + (GetTickCount - start_time).ToString); start_time := GetTickCount;
for tempStr in MyList do
begin
//只有使用才能看到效果
tempList2.Add(tempStr);
end;
Memo1.Lines.Add('fot in 耗时:' + (GetTickCount - start_time).ToString);
finally
MyList.Free;
tempList1.Free;
tempList2.Free;
end;
end; end.
效率差不多,其它的就不测试了。 总之以后 不要把思维局限于 for to 而是 多用 for in
泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable的更多相关文章
- JAVA中ArrayList与LinkedList的区别以及对应List使用foreach与使用下标遍历的效率问题
近期在做一个对接京东的电商平台,所以对各个地方的效率考虑的比较多,今天深挖了一下ArrayList与LinkedList的区别以及对应List使用foreach与使用下标遍历的效率问题,首先说一下两种 ...
- C#中使用泛型对照使用通用基础类型效率减少近一倍
C#中使用泛型对照使用通用基础类型效率减少近一倍 以下是測试结果: CSharp class and generic TotalMilliseconds: 270772.9229CSharp g ...
- Java中HashMap(泛型嵌套)的遍历
//Studnet package yzhou.gen03; public class Student<T> { private T score; public T getScore() ...
- JS 中的数组遍历方式效率比较
JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代码如下: ; j < ...
- C++11中对容器的各种循环遍历的效率比较
#include "CycleTimeTst.h" #include <string> #include <vector> #include <lis ...
- 使用泛型与不使用泛型的Map的遍历
https://www.cnblogs.com/fqfanqi/p/6187085.html
- PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)
* 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数 ...
- 专题三、ArrayList遍历方式以及效率比较
一.遍历方式 ArrayList支持三种遍历方式. 1.第一种,随机访问,它是通过索引值去遍历 由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素. 代码如下: ...
- map遍历的几种方式和效率问题
一.map遍历的效率 先创建一个map,添加好数据: Map<String, String> map = new HashMap<>();for (int i = 0; i & ...
随机推荐
- 解题:AHOI2017/HNOI2017 礼物
题面 先不管旋转操作,只考虑增加亮度这个操作.显然这个玩意的影响是相对于$x,y$固定的,所以可以枚举增加的亮度然后O(1)算出来.为了方便我们把这个操作换种方法表示,只让一个手环改变$[-m,m]$ ...
- csu1377Putter && HOJ12816
链接:(csu)http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1377 (HOJ)http://49.123.82.55/online/?actio ...
- Docker图形界面管理之Portainer
介绍 Portainer是一个开源.轻量级Docker管理用户界面,基于Docker API,可管理Docker主机或Swarm集群,支持最新版Docker和Swarm模式.官方文档 https:// ...
- Ansible11:变量详解
目录 简单说明 一.在Inventory中定义变量 二.在Playbook中定义变量 1.通过vars关键字定义 2.通过vars_files关键字引入变量文件 3.通过vars_prompt来实现人 ...
- Java poi读取,写入Excel2007
Java poi读取,写入Excel2007 相关阅读:poi读写Excel2003:http://www.cnblogs.com/gavinYang/p/3576739.htmljxl读写excel ...
- memcmp 和 memcpy使用
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- CSS中的选择器(笔记)
1.通配符选择器(*):通配符选择器是用来选择所有元素,也可以选择某个元素下的所有元素.所有浏览器都支持通配符选择器. ;;} .dome *{padding: 2px;} 2.元素选择器(Ele): ...
- datagrid时间插件
jquery easyui日期控件中,在页面里用JS拿到设立的日期值的方法 链接:http://blog.csdn.net/liweibin_/article/details/13509917 jqu ...
- kafka入门(2)- 环境部署
部署Zookeeper(单机/集群) 1.下载安装文件: http://mirror.bit.edu.cn/apache/zookeeper/ 2.解压文件(本文解压到 D:\zookeeper-3. ...
- Sql Server 逻辑文件 '' 不是数据库 '' 的一部分。请使用 RESTORE FILELISTONLY 来列出逻辑文件名。
当使用语句还原数据库时,报如下错误: 消息 3234,级别 16,状态 2,第 29 行逻辑文件 'LenborMealOrder_Base_2017' 不是数据库 'Members_01' 的一部分 ...