## 用对象获取列表反集，速度飞快

tmplinshi
Posts: 1304
Joined: 01 Oct 2013, 14:57

### 用对象获取列表反集，速度飞快

Code: Select all

``````SetBatchLines -1

; 生成 10 万个数字
Loop, 100000
fullList .= "," A_Index
fullList := Trim(fullList, ",")

; 生成 6 万个数字
Loop, 60000
subList .= "," A_Index
subList  := Trim(subList, ",")

; 获取 4 万个反集数字

; 方法一: InStr()
TickCount_Start := A_TickCount ; 计时开始

resultList := ""
Loop, Parse, fullList, CSV
If !InStr(subList, A_LoopField)
resultList .= "," A_LoopField

MsgBox, % "耗时: " (A_TickCount-TickCount_Start)/1000 " 秒" ; 我这里显示“耗时: 38.516000 秒”

; 方法二: Object
TickCount_Start := A_TickCount ; 计时开始

; 把 6 万个数字列表转为对象
oSubList := {}
Loop, Parse, subList, CSV
oSubList[A_LoopField] := 1

; 开始...
resultList := ""
Loop, Parse, fullList, CSV
If !oSubList[A_LoopField]
resultList .= "," A_LoopField

MsgBox, % "耗时: " (A_TickCount-TickCount_Start)/1000 " 秒" ; 我这里显示“耗时: 0.125000 秒”``````
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

### Re: 用对象获取列表反集，速度飞快

• 如果期望的返回结果也是对象(数组)，可以oResultList:=objClone(oFullList)，遍历oSubList并删除oResultList中相应项。
• 感觉尽量用对象。且由于要遍历（而非索引SubList，可使用StrSplit进行列表至对象的转换。

Code: Select all

``````oSubList:=StrSplit(SubList,",")
oResultList:=objClone(oFullList)
for _,v in oSubList
objRemove(oResultList,v)``````

hitman
Posts: 11
Joined: 10 Aug 2014, 06:47