gto250 发表于 2011-4-14 20:46:21

字符串比较的效率问题[已解决]

本帖最后由 gto250 于 2011-5-11 18:07 编辑

$begin = TimerInit()
$a="123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430"
$b="95412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123"
$a1=StringUpper($a)
$b1=StringUpper($b)

For $n=1 To 500
Dim $c
For $i=1 To StringLen($a1) Step 4
$a_1=StringMid($a1,$i,4)       
$b_1=StringMid($b1,$i,4)       

$d=StringCompare($a_1,$b_1)
        If $d=0 Then
                $c=$c&"0"
        Else
                $c=$c&"1"
        EndIf
Next
Next
$dif = TimerDiff($begin)

MsgBox(0,"",$dif/1000)


这是我写的代码,循环500次对两个字符串进行比较,每4个字符为1组,相同的设置为1,不同的设置为0
我的机子4G内存 2.6双核
运行时间要8秒左右!
兄弟们有什么好的算法没!

love5173 发表于 2011-4-14 21:15:48

回复 1# gto250
我认为处理的慢是慢在了从你的$a $b抽取字符上了,我提议把$a $b做成数组进行对比 第三个数组储存结果,最后把数组转换成字符串,你认为可行吗,可行的话 我上代码

afan 发表于 2011-4-14 21:22:27

本帖最后由 afan 于 2011-4-14 21:24 编辑

直接比对试下$begin = TimerInit()
$a = "123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430"
$b = "95412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123"
$a = StringUpper($a)
$b = StringUpper($b)
Dim $c
For $n = 1 To 500
        For $i = 1 To StringLen($a) Step 4
                If StringMid($a, $i, 4) == StringMid($b, $i, 4) Then
                        $c &= "1"
                Else
                        $c &= "0"
                EndIf
        Next
Next
$dif = TimerDiff($begin)
MsgBox(0, '', $dif / 1000)另外,你上面的“相同的设置为1,不同的设置为0”貌似反了吧

love5173 发表于 2011-4-14 21:59:53

回复 3# afan

大叔你误会我的意思了,他在循环的时候存在重复从一个大的字符串提取字串的过程,我认为慢在这里了,我的意思是用正则一次提取出500个字串 然后$a==$b 存入$c
最后处理数组$c 转化成字符串

netegg 发表于 2011-4-14 22:04:01

本帖最后由 netegg 于 2011-4-14 22:29 编辑

这个最后要什么结果?$begin = TimerInit()
$a = "123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430"
$b = "95412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123"
$a = Stringtobinary($a);_stringtohex()
$b = Stringtobinary($b);_stringtohex()
$c =bitand($a, $b)
$dif = TimerDiff($begin)
MsgBox(0, '', $dif / 1000)大概可以,不过有点小问题。stringtobinary好像有位数限制,思路而已

afan 发表于 2011-4-14 22:07:27

回复 4# love5173


    我那是回的LZ的,没想到你已经在2楼了~
循环500次我想只是便于比较效率而已,并非一次提取500个字符…

我那里简单处理了一下,LZ的要8秒,我的1.2秒

gto250 发表于 2011-4-14 22:19:53

回复 6# afan

的确快了很多,1.2秒左右

再去除
$a = StringUpper($a)
$b = StringUpper($b)

这两句以后,就要1.5S左右了,这是什么原因呢?

love5173 发表于 2011-4-14 22:26:18

本帖最后由 love5173 于 2011-4-14 22:29 编辑

$begin = TimerInit()
$a="123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430"
$b="95412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123"
$a1=StringUpper($a)
$b1=StringUpper($b)
$aa=StringRegExp($a1,'\w{4}',3)
$bb=StringRegExp($b1,'\w{4}',3)
;_ArrayDisplay ($aa)

For $n=1 To 500
Dim $c
;Global $i
For $i=0 To UBound($aa)-1
;$a_1=StringMid($a1,$i,4)      
;$b_1=StringMid($b1,$i,4)      

$d=StringCompare($aa[$i],$bb[$i])
      If $d=0 Then
                $c=$c&"0"
      Else
                $c=$c&"1"
      EndIf
Next
Next
$dif = TimerDiff($begin)

MsgBox(0,"",$dif/1000)这个是我的,貌似也挺慢的,我CPU不行,帮我测试下时间,我的cpu跑下来要21秒,我这cpu什么水平,显示是双核2.5,4G内存,HP笔记本

gto250 发表于 2011-4-14 22:28:09

回复 8# love5173
6.75秒

afan 发表于 2011-4-14 22:28:52

回复 7# gto250


    这个不清楚~ 可能是如果去掉这两行将加重后面 == 比对时的负担~

netegg 发表于 2011-4-14 22:32:17

没搞明白,lz到底是要比对完的字符串还是就想知道相同与否

love5173 发表于 2011-4-14 22:34:40

本帖最后由 love5173 于 2011-4-14 22:38 编辑

$begin = TimerInit()
$a="123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430123456789012345698765432107654301234567890123456987654321076543012345678901234569876543210765430"
$b="95412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123580125789685469870125493159541235801257896854698701254931595412358012578968546987012549315954123"
$a1=StringUpper($a)
$b1=StringUpper($b)
$aa=StringRegExp($a1,'\w{4}',3)
$bb=StringRegExp($b1,'\w{4}',3)
;_ArrayDisplay ($aa)

For $n=1 To 500
Dim $c
;Global $i
For $i=0 To UBound($aa)-1
;$a_1=StringMid($a1,$i,4)      
;$b_1=StringMid($b1,$i,4)      
Dim $d
If $aa[$i] == $bb[$i] Then

      If $d=0 Then
                $c=$c&"0"
      Else
                $c=$c&"1"
                        EndIf
                        EndIf
Next
Next
$dif = TimerDiff($begin)

MsgBox(0,"",$dif/1000)试试这个,偷学大叔的,应该比大叔的快,貌似如果把c换成数组处理能更快

netegg 发表于 2011-4-14 22:36:47

本帖最后由 netegg 于 2011-4-14 22:37 编辑

0.0002秒多点

love5173 发表于 2011-4-14 22:40:28

回复 13# netegg
你用的什么本子啊,我的是HP8510W2.5 双核 ,4G内存 为什么跑起来差距这么大

netegg 发表于 2011-4-14 22:41:49

本帖最后由 netegg 于 2011-4-14 22:43 编辑

用位运算不用比对呀,不管怎么节省条件,读取字符串本身就不是脚本的强项
页: [1] 2 3
查看完整版本: 字符串比较的效率问题[已解决]