怎么优化vb for循环的性能 循环70000次以上

在text3中有70000多行 逐行读取判断其长度跟是否包含.com 符合就输出到另一text里
我这样写了 但太慢 循环要1个多小时。。
麻烦有高手优化下。。

arr = Split(Text3.Text, vbCrLf)
hang = (UBound(arr))
MsgBox hang
Label4.Caption = hang

For L = 0 To hang
st = Split(Text3.Text, vbCrLf)(L)

If st = "" Then Exit For
If RealLen(Mid(st, 1, InStr(1, st, ".") - 1)) < 6 And InStr(st, ".com") > 0 Then
Text1.Text = Text1.Text & st & vbCrLf
End If
Label5.Caption = L
DoEvents

Next L

Function RealLen(txt)
Dim x, y, i
x = Len(txt)
y = 0
For i = 1 To x
If Asc(Mid(txt, i, 1)) < 0 Or Asc(Mid(txt, i, 1)) > 255 Then
y = y + 2 '双字节
Else
y = y + 1 'ascii码字符
End If
Next
RealLen = y
End Function

这程序效率肯定不行。
1. 你的ReadLen(txt)太业余了,应该用Regex。
2. 你有一个假设,就是.com 出现在同一行内。 如果 .c 出现在第一行的末尾,om出现在第二行的开头,就被漏掉了。追问

能给段代码不? 谢谢

追答

我对VB.net 不是特别熟。这是在网上找的。
Sub Main()
Dim re As Regex = New Regex(".com")
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-12
arr = Split(Text3.Text, vbCrLf)
hang = (UBound(arr))
MsgBox hang
Label4.Caption = hang

Dim str1 As String '添加

For L = 0 To hang
st = arr(L) '修改

If st = "" Then Exit For
If RealLen(Mid(st, 1, InStr(1, st, ".") - 1)) < 6 And InStr(st, ".com") > 0 Then
str1 = str1 & st & vbCrLf '修改
End If
Label5.Caption = L
DoEvents

Next L
Text1.Text = str1 '添加
第2个回答  2011-04-14
1,把text3中的内容保存到记事本中,
2,然后用open " 记事本" for input as #1 读取打开记事本;
3,用lineinput按行读取记事本内容;
4,用instr()函数判断每行内容是否包含.com,如果是则保存到另text中
5,用do...loop while not EOF(#1)读取所有的行
6,关闭记事本
第3个回答  2011-04-12
把DoEvents去掉,应该会快一点追问

那看不到状态的输出了吧?

相似回答