String 在VBScript中连接字符串的快速方法是什么

String 在VBScript中连接字符串的快速方法是什么,string,performance,concatenation,String,Performance,Concatenation,字符串连接时间几乎是连接量和目标字符串总大小的指数函数,这是由于每次出现以下语句时内存重新定位造成的 myStr=myStr+anotherString字符串连接时间几乎是连接量和目标字符串的总大小的指数函数,这是由于每次内存重新定位时出现以下语句 myStr=myStr+另一个字符串 下面的代码是基于 . 其思想是使用对象的单向链表来防止昂贵的内存重新分配。 我优化了大量串联的代码,以减少列表中的元素数量,因为过多的元素数量会导致IIS崩溃 用例连接多次,然后得到结果字符串 在我的测试中,我能

字符串连接时间几乎是连接量和目标字符串总大小的指数函数,这是由于每次出现以下语句时内存重新定位造成的
myStr=myStr+anotherString

字符串连接时间几乎是连接量和目标字符串的总大小的指数函数,这是由于每次内存重新定位时出现以下语句 myStr=myStr+另一个字符串

下面的代码是基于 . 其思想是使用对象的单向链表来防止昂贵的内存重新分配。 我优化了大量串联的代码,以减少列表中的元素数量,因为过多的元素数量会导致IIS崩溃

用例连接多次,然后得到结果字符串
在我的测试中,我能够连接超过10000个字符串,总计25MB

 'Example of use:
 dim str="Quite a long-long-long-long-long-long-long string here"
 dim oStr,resultString
 set oStr= new FStringCat        
    for i= 1 to 100000
            oStr.Add(str)             
     Next
  resultString = oStr.ToString()  ' this will take some time


 '====================================
class FStringCat
private sp
private ep
private l
private accum
private  maxLen
private objCount
private  bigString   
private switched
public Length
private sub Class_Initialize
    l=0
    accum=""
    set sp=nothing
    Length=0
    maxLen=2800
    objCount=0

end sub

public sub Add(what)
    Length=Length+len(what)
    if switched then
            bigString.Add(what)
            exit sub
    end if

  if objCount>1200  then  ' prevent IIS crash
        set bigString=new  FastStringHV
        bigString.Add(accum)
        bigString.Add(what)
        switched=true
        exit sub
    end if

    accum=accum & what

    if len(accum)> maxLen then   ' originally 2800
        objCount=objCount+1
        if objCount>1000 then  maxLen=15000
        if(sp is nothing) then
            set ep=new XLink
            set sp=ep
        else
            dim oep
            set oep=ep
            set ep=new XLink
            set oep.nextXLink=ep
        end if
        ep.datum=accum
        accum=""
        l=l+1
    end if
end sub

public function ToString()
    if l=0 then
        toString=accum
        exit function
    end if
    ep.datum=ep.datum & accum

    while l>1 
        dim ptr:     set ptr=sp
        dim nsp :   set nsp=new XLink
        dim nep :     set nep=nsp
        dim nl :     nl=0

        while not (ptr is nothing) 
            if  nep.datum=""  then
                nep.datum=ptr.datum
                nl=nl+1
            else
                if ptr.datum<>"" then nep.datum=nep.datum & ptr.datum
                set nep.nextXLink=new XLink
                set nep=nep.nextXLink
            end if
            set ptr=ptr.nextXLink
        wend
        set sp=nsp
        set ep=nep
        l=nl
    wend
    if  switched then
        ToString=sp.datum +bigString.ToString()
    else
        ToString=sp.datum 
    end if
end function
末级

private sub Class_Initialize
    str=""
    longStr=""
end sub

public sub Add(what)
    if len(str) > 2800 then
        longStr=longStr+str
        str=""
    end if
    str=str+what
end sub 

public function ToString()
    if len(longStr) = 0 then
        ToString=str
    elseif len(str)=0 then
            ToString=longStr
    else
        ToString=longStr+str
    end if
end function