本帖最后由 vuivui 于 2019-9-5 15:41 编辑
测试结果:
方法一(Assign):
Local $f, $f1, $s, $n = 0$f = FileRead("题库.txt") $s = StringRegExpReplace($f,'(?m)^\d+.\s*', "") $s = StringSplit($s, @CRLF, 1)
Local $t = TimerInit() For $i = 1 To $s[0] IfIsDeclared($s[$i]) Then ContinueLoop Assign($s[$i],0) $f1&= $s[$i] & @CRLF $n+= 1 Next MsgBox(1, "用时:" & TimerDiff($t),"重行行数:"& $s[0] - $n) $file = FileOpen("f1.txt", 2) FileWrite($file, $f1) FileClose($file) ShellExecute("f1.txt")
方法二(字符逐个比较):
Local $f, $f1, $s, $n = 0
$f = FileRead("题库.txt")
$s = StringRegExpReplace($f, '(?m)^\d+.\s*', "")
$s = StringSplit($s, @CRLF, 1)
Local $t = TimerInit()
Local $slen[$s[0] + 1]
For $i = 1 To $s[0]
$slen[$i] = StringLen($s[$i])
Next
For $i = 1 To $s[0]
If $slen[$i] = 0 Then ContinueLoop
For $j = $i + 1 To $s[0]
;If $slen[$i] = $slen[$j] And $s[$i]==$s[$j] Then $slen[$j]=0
If $slen[$i] = $slen[$j] Then
For $k = 1 To $slen[$i]
;If BitXOR(AscW(StringMid($s[$i], $k, 1)), AscW(StringMid($s[$j], $k, 1))) Then ExitLoop
If StringMid($s[$i], $k, 1) <> StringMid($s[$j], $k, 1) Then ExitLoop
If $k = $slen[$i] Then $slen[$j] = 0
Next
EndIf
Next
$f1 &= $s[$i] & @CRLF
$n += 1
Next
MsgBox(1, "用时:" & TimerDiff($t), "重行行数:" & $s[0] - $n)
$file = FileOpen("f1.txt", 2)
FileWrite($file, $f1)
FileClose($file)
ShellExecute("f1.txt")
方法三(字符串比较):
Local $f, $f1, $s, $n = 0
$f = FileRead("题库.txt")
$s = StringRegExpReplace($f, '(?m)^\d+.\s*', "")
$s = StringSplit($s, @CRLF, 1)
Local $t = TimerInit()
Local $slen[$s[0] + 1]
For $i = 1 To $s[0]
$slen[$i] = StringLen($s[$i])
Next
For $i = 1 To $s[0]
If $slen[$i] = 0 Then ContinueLoop
For $j = $i + 1 To $s[0]
If $slen[$i] = $slen[$j] And $s[$i]==$s[$j] Then $slen[$j]=0
Next
$f1 &= $s[$i] & @CRLF
$n += 1
Next
MsgBox(1, "用时:" & TimerDiff($t), "重行行数:" & $s[0] - $n)
$file = FileOpen("f1.txt", 2)
FileWrite($file, $f1)
FileClose($file)
ShellExecute("f1.txt")
方法四(字典对象):
Local $f, $f1, $s, $n = 0
$f = FileRead("题库.txt")
$s = StringRegExpReplace($f, '(?m)^\d+.\s*', "")
$s = StringSplit($s, @CRLF, 1)
Local $t = TimerInit()
Local $o = ObjCreate('Scripting.Dictionary')
For $i = 1 To $s[0]
If $o.Exists($s[$i]) Then ContinueLoop
$o.Add($s[$i], 0)
$f1 &= $s[$i] & @CRLF
$n += 1
Next
MsgBox(1, "用时:" & TimerDiff($t), "重行行数:" & $s[0] - $n)
$file = FileOpen("f1.txt", 2)
FileWrite($file, $f1)
FileClose($file)
ShellExecute("f1.txt")
把方法一(Assign)用时设为1,用时比例如下:
结果同预想的差不多,字典对象法用时是Assign法的3倍,字符串比较法看起来用时多,实际在我办公的老爷子机上也就用了4秒。
因为楼主提供的文本行字符串长,所以用时多,简单文本的处理这几个方法都能用,字符比较与字符串比较方法对理解au3编程有好处,实际碰到的问题往往不是删除重复行那么简单。
异或法注释在方法二中,慢了一点点,这个方法是C语言常用优化手段,楼主文本的行首数字都作了删除处理,因为这里只是为了检测去除重复行的速度,几个方法的去重数有出入,是换行的计算问题,懒得作修正了。
|