泛型 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 & ...
随机推荐
- golang单元测试
使用testing进行单元测试 golang的测试库testing 测试文件与被测试文件在同一个包中 测试文件名为被测试文件名(去后缀)_test.go 测试用例函数以Test开头,TestFunc1 ...
- java中的date类型转换为js中的日期显示 我改
function dateChange(javaDate){ if(javaDate){ return javaDate.substr(0,10).replace(/-/g,"/" ...
- salt源码安装
salt是什么? 一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, 使其可以用于编配, 远 ...
- Codeforces Round #302 (Div. 2) C 简单dp
C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...
- 利用solr实现商品的搜索功能
后期补充: 为什么要用solr服务,为什么要用luncence? 问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据 ...
- 八、java常用类
目录 一.字符串相关类 String类 StringBuffer类 二.基本数据类型包装类 三.Math类 四.File类 五.枚举类 一.字符串相关类 1.String类 java.lang.Str ...
- Docker Swarm高可用性
一.前言 在Docker Swarm集群中,Swarm manager负责管理整个集群,如果管理节点manager出现故障,虽然不会影响现有的服务和工作节点,但是我们不能继续管理我们的docker s ...
- python net-snmp使用
安装 官网:http://www.net-snmp.org/download.html 环境:CentOS 6.6 + python 2.7.10 1.下载安装包 net-snmp-5.6.2.1.t ...
- Hadoop生态圈-Hive的自定义函数之UDAF(User-Defined Aggregation Function)
Hadoop生态圈-Hive的自定义函数之UDAF(User-Defined Aggregation Function) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- grep与正则表达式详解和实例
转载自:http://www.jb51.net/article/31207.htm grep 工具,以前介绍过. grep -[acinv] '搜索内容串' filename -a 以文本文件方式搜索 ...