Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / VBScript

High Speed String Concatenation Using VBScript

Rate me:
Please Sign up or sign in to vote.
2.94/5 (6 votes)
13 Apr 2007CPOL 25.4K   8  
It has been said that large scale creation of long strings from small strings is not practical in VBScript - NOT SO

Introduction

Some time ago, I posted about high speed string concatenation in JScript, here is the VBScript equivalent.

This post is not really a full post. Don't jump to give it a -1 rating. This is here as an addendum to a previous article which - in great detail - shows the thinking behind this but uses JScript. To get the background, go here.

Below is the code. The only real difference between this and the JScript is that in VBScript, arrays are not objects. This means that I have had to create an explicit class to create the objects for the linked list that holds the string fragments. Also please note that in VBScript, arrays are of fixed length. If you redim an array, you actually perform a copy operation (the old one is thrown away and you get a new array). This means that the split/join technique discussed in the messages with the JScript post cannot work in VBScript.

VBScript
class XLink
    public datum
    public nextXLink
    
    private sub Class_Initialize
      set nextXLink=nothing
      datum=""
    end sub
    
end class

class FStringCat
    private sp
    private ep
    private l
    private accum

    private sub Class_Initialize
        l=0
        accum=""
        set sp=nothing
    end sub
    
    public sub push(what)
        accum=accum & what
        if len(accum)>2800 then
            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
        toString=sp.datum
    end function
    
end class

' Some example code
WScript.echo("Starting")
dim ts
set ts=new FStringCat
WScript.echo("Created FStringCat")
dim i
for i=1 to 1000
    ts.push("Hi there: " & i & vbcrlf)
next
WScript.echo("Results")
WScript.echo(ts.toString())

The original nerds-central post is here. For more like this, check out Nerds-Central.

History

  • 13th April, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
I am now a Software Systems Developer - Senior Principal at Micro Focus Plc. I am honoured to work in a team developing new compiler and runtime technology for Micro Focus.

My past includes a Ph.D. in computational quantum mechanics, software consultancy and several/various software development and architecture positions.

For more - see

blog: http://nerds-central.blogspot.com

twitter: http://twitter.com/alexturner

Comments and Discussions

 
-- There are no messages in this forum --