7,762
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command3_Click()
Dim ReadArray() As String
Dim lngArraySize As Long
Dim strTmp As String
Dim fs As Integer
Dim row As Long
Dim i As Long
Dim j As Long
Dim index As Long
Dim iscompositor As Boolean
List1.Clear
Dim savetime As Long
savetime = GetTickCount
'========== 把文本文件以行为单位读入字符串数组 ==========
row = 0
ReDim ReadArray(row)
fs = FreeFile
Open App.Path & "\" & Text1.Text For Input As #fs
Do While Not EOF(fs)
Line Input #fs, strTmp
ReadArray(row) = strTmp
row = row + 1
ReDim Preserve ReadArray(row)
Loop
Close #fs
lngArraySize = row - 1
ReDim Preserve ReadArray(lngArraySize)
Dim readfiletime As Long
readfiletime = GetTickCount
'========== 对数组进行排序 ==========
'冒泡法
'Call compositor_ebullient(ReadArray)
Dim mpftime As Long
mpftime = GetTickCount
'选择法
'Call compositor_select(ReadArray)
Dim xzftime As Long
xzftime = GetTickCount
'快速法
Call compositor_quick(ReadArray, 0, lngArraySize)
Dim ksftime As Long
ksftime = GetTickCount
'插入法
' Call compositor_insert(ReadArray)
Dim crftime As Long
crftime = GetTickCount
'希尔法
' Call compositor_shell(ReadArray)
Dim xrftime As Long
xrftime = GetTickCount
'========== 对数组进行除重 ==========
'除重
Call kill_repeated(ReadArray)
Dim ccftime As Long
ccftime = GetTickCount
Dim compositortime As Long
compositortime = GetTickCount
'========== 将排序好的数组输出 ==========
lngArraySize = UBound(ReadArray)
For i = 0 To lngArraySize
List1.AddItem ReadArray(i)
Next i
Dim overtime As Long
overtime = GetTickCount
MsgBox "总耗时:" & overtime - savetime & " 毫秒" & vbCrLf & _
"读文件:" & readfiletime - savetime & " 毫秒" & vbCrLf & _
"冒泡法:" & mpftime - readfiletime & " 毫秒" & vbCrLf & _
"选择法:" & xzftime - mpftime & " 毫秒" & vbCrLf & _
"快速法:" & ksftime - xzftime & " 毫秒" & vbCrLf & _
"插入法:" & crftime - ksftime & " 毫秒" & vbCrLf & _
"希尔法:" & xrftime - crftime & " 毫秒" & vbCrLf & _
"除重复:" & ccftime - xrftime & " 毫秒" & vbCrLf & _
"加数据:" & overtime - compositortime & " 毫秒"
End Sub
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
' 需要引用:Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim objDict As New Dictionary
Dim arrBuf() As String
Dim arrOut() As String
Dim arrIndex() As Long
Dim i&, p&, m&, strTemp$
Dim savetime As Long
savetime = GetTickCount
Open App.Path & "\" & Text1.Text For Binary As #1
arrBuf = Split(Input(LOF(1), 1), vbCrLf)
'筛选、除重
p = -1
m = UBound(arrBuf)
ReDim arrOut(m)
For i = 0 To m
strTemp = arrBuf(i)
If (Len(strTemp)) Then
If (Not objDict.Exists(strTemp)) Then
p = p + 1
objDict.Add strTemp, p
arrOut(p) = strTemp
End If
End If
Next
Close #1
objDict.RemoveAll
Set objDict = Nothing
If (p = -1) Then
MsgBox "没有内容。", vbExclamation
Exit Sub
End If
'乱序输出
ReDim arrIndex(p)
For i = 0 To p
arrIndex(i) = i
Next
Randomize
m = p
For i = 0 To p
p = Rnd() * m
List1.AddItem arrOut(arrIndex(p))
arrIndex(p) = arrIndex(m)
m = m - 1
Next
MsgBox "耗时:" & GetTickCount - savetime & " 毫秒"
End Sub
Private Sub Command2_Click()
Dim test As String
Dim i As Long
Dim savetime As Long
savetime = GetTickCount
Open App.Path & "\" & Text1.Text For Output As #1
For i = 1 To 100000
test = String(8 - Len(Hex(i)), "0") & Hex(i)
Print #1, test
Next i
Close #1
MsgBox "耗时:" & GetTickCount - savetime & " 毫秒"
End Sub
Option Explicit
' 需要引用:Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim objDict As New Dictionary
Dim arrBuf() As String
Dim arrOut() As String
Dim arrIndex() As Long
Dim i&, p&, m&, strTemp$
Open App.Path & "\" & Text1.Text For Binary As #1
arrBuf = Split(Input(LOF(1), 1), vbCrLf)
'筛选、除重
p = -1
m = UBound(arrBuf)
ReDim arrOut(m)
For i = 0 To m
strTemp = arrBuf(i)
If (Len(strTemp)) Then
If (Not objDict.Exists(strTemp)) Then
p = p + 1
objDict.Add strTemp, p
arrOut(p) = strTemp
End If
End If
Next
Close
objDict.RemoveAll
Set objDict = Nothing
If (p = -1) Then
MsgBox "没有内容。", vbExclamation
Exit Sub
End If
'乱序输出
ReDim arrIndex(p)
For i = 0 To p
arrIndex(i) = i
Next
Randomize
m = p
For i = 0 To p
p = Rnd() * m
List1.AddItem arrOut(arrIndex(p))
arrIndex(p) = arrIndex(m)
m = m - 1
Next
End Sub
Private Sub kill_repeated(strArray() As String)
Dim max As Long, _
min As Long, _
i As Long, _
j As Long, _
count As Long, _
strTmp As String
max = UBound(strArray)
count = max
min = LBound(strArray)
j = 0
For i = min + 1 To max
strTmp = strArray(i - 1)
If Len(strTmp) = 0 Then
strArray(i - 1) = strArray(i + j)
j = j + 1
i = i - 1
count = count - 1
Else
If strArray(i - 1) = strArray(i + j) Then
j = j + 1
i = i - 1
count = count - 1
Else
strArray(i) = strArray(i + j)
End If
End If
If i >= count Then Exit For
Next i
ReDim Preserve strArray(count)
End Sub
这个赛选过程完全是基于排序好的数据进行筛选,所以根本不需要什么时间,我这里的10W条全反序数据被弄正序之后,只用了62 ms就完成了这种重复筛选。我测试的数据是这样来的。
Private Sub Command2_Click()
Dim test As String
Dim i As Long
Dim j As Long
Dim savetime As Long
savetime = GetTickCount
Open App.Path & "\" & Text1.Text For Output As #1
For i = 0 To 9999
test = String(8 - Len(Hex(10000 - i)), "0") & Hex(10000 - i)
For j = 1 To 10
Print #1, test
Next j
Next i
Close #1
MsgBox "耗时:" & GetTickCount - savetime & " 毫秒"
End Sub