泛型 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 & ...
随机推荐
- Android资源混淆 + 混淆忽略 .so库
安装包立减1M--微信Android资源混淆打包工具http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=208135658&idx ...
- centos6.5搭建LVS+Keepalived
1.配置LVS负载调度器 (1)为eth0配置IP地址,为eth0:0配置VIP地址. vi /etc/sysconfig/network-scripts/ifcfg-eth0 …… DEVICE=e ...
- hibernate的懒加载
WHY? WHAT? HOW? 所谓懒加载(lazy)就是延时加载,延迟加载.即不是不加载,而是在需要的时候才加载. 什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载. 至于为什么要用懒加载呢 ...
- Jmeter之性能测试
Jmeter除了可以做接口测试外,还可以做性能测试.在 Jmeter中做性能测试,需要做如下相关设置 图片中有10个线程,Ramp-Up Period(in seconds)=1,那么线程的启动时间间 ...
- location.href跳转测试
测试代码 <script type="text/javascript"> function ToUrl(x){ location.href=x; } </scri ...
- openstack中的server
一.HTTP server 主要是horizon模块,horizon是基于Python Django搭建的web应用,其运行于Apache网络服务器上(当然也可以运行在其他web服务器上),主要功能就 ...
- JavaScript中replace()方法的第二个参数解析
语法 string.replace(searchvalue,newvalue) 参数值 searchvalue 必须.规定子字符串或要替换的模式的 RegExp 对象.请注意,如果该值是一个字符串,则 ...
- linux split 切割大文件
语法: split [-l <行数>] [-b <字节>] [-C <字节>] [要切割的目标文件] [输出文件名前缀] 说明: -l <行数> 指定 ...
- sql 存储时空格转成问号问题
最近做系统,从邮件中导出邮件,上传到系统中,遇到一个奇葩的问题,如下: 通过本地文件看,文件名中是一个空格,上传至数据库后,展示就变成了问号,究其原因,发现是一个特殊字符导致: 最近认真去查了一下这个 ...
- Redis实战(四)CentOS 7上Redis哨兵
什么是哨兵 顾名思义,哨兵的作用就是对Redis的系统的运行情况的监控,它是一个独立进程.它的功能有2个: 1. 监控主数据库和从数据库是否运行正常: 2. 主数据出现故障后自动将从数据库转化为主数据 ...