Efficient string concatination 
Author Message
 Efficient string concatination

I'm trying to take an array of Bytes, convert each byte into a
character and store it all in a String. What I've been using is:

For LongCount = 0 To LongByteCount - 1
    HeadData = HeadData & Chr(ByteArray(LongCount))
Next

I've found that this does a very inneficient job of storing the
characters. I think it's creating some temp variable every time it does
a concatination which becomes very expensive as HeadData becomes large.
Any suggestions? Thanks!

-Dan

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination

?I'm trying to take an array of Bytes, convert each byte into a
?character and store it all in a String. What I've been using is:
?
?For LongCount = 0 To LongByteCount - 1
?    HeadData = HeadData & Chr(ByteArray(LongCount))
?Next
?
?I've found that this does a very inneficient job of storing the
?characters. I think it's creating some temp variable every time it does
?a concatination which becomes very expensive as HeadData becomes large.
?Any suggestions? Thanks!

Hi Dan,

Does this do what you want?

   HeadData = StrConv(ByteArray, vbUnicode)

--
Paul Marshall



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination

Since you're starting w/ a byte array, the prior respondent's approach is
best.

In general, however, VB does do a lot of BSTR allocation when concatenating
strings, and that can lead to performance issues.  In normal cases
performance is affected by 2 factors: length of concatenated strings, and
order of concatenation.

Turns out that the OLE subsystem maintains a string cache of 60K or so, so
concatenations that fit in this size are quick enough.  Longer strings are
heap-allocated and take considerably longer to effect.

Also related to the above, the performance of a statement such as:

sLongString = sLongString & " " & vbCrlf

can be significantly increased by specifying the concatenation order so that
small strings are concatenated first.  Like so:

sLongString = sLongString & (" " & vbcrlf)

Finally, the quickest way to concatenate strings in VB6 is to use the Join()
function.  Of course, since Join() takes an array arg, we have to deal with
dimensioning an array of adequate size in the first place.  This can be done
using repeated ReDim's (a bad idea), or Dim-ing a large array then messing
with its SAFEARRAY info so Join() sees only the elements you want it to.

The above is from Matt Curland's 'Advanced VB 6' from Addison Wesley.
Please refer to it for details.

--A



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination
Personally I prefer

Public Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Sub BytesToString(ByRef bytArray() As Byte, ByRef strOutput As String)
Dim lngLen As Long
lngLen = Ubound(bytArray) - LBound(bytArray) + 1
strOutput = Space$(lngLen)
CopyMemory ByVal strOutput, bytArray(LBound(bytArray)), lngLen
End Sub

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://computers.kingsoft-denmark.com/


Quote:

> Since you're starting w/ a byte array, the prior respondent's approach is
> best.

> In general, however, VB does do a lot of BSTR allocation when
concatenating
> strings, and that can lead to performance issues.  In normal cases
> performance is affected by 2 factors: length of concatenated strings, and
> order of concatenation.

> Turns out that the OLE subsystem maintains a string cache of 60K or so, so
> concatenations that fit in this size are quick enough.  Longer strings are
> heap-allocated and take considerably longer to effect.

> Also related to the above, the performance of a statement such as:

> sLongString = sLongString & " " & vbCrlf

> can be significantly increased by specifying the concatenation order so
that
> small strings are concatenated first.  Like so:

> sLongString = sLongString & (" " & vbcrlf)

> Finally, the quickest way to concatenate strings in VB6 is to use the
Join()
> function.  Of course, since Join() takes an array arg, we have to deal
with
> dimensioning an array of adequate size in the first place.  This can be
done
> using repeated ReDim's (a bad idea), or Dim-ing a large array then messing
> with its SAFEARRAY info so Join() sees only the elements you want it to.

> The above is from Matt Curland's 'Advanced VB 6' from Addison Wesley.
> Please refer to it for details.

> --A



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination
Surely this will push ANSI bytes into an ANSI copy of a UNICODE string
which VB will then re-convert into UNICODE

In general StrConv looks better for this  type of operation.

If concatenating  true strings (not byte array to string) and speed is
critical then the trick is to pre-format the final string, and then
insert the smaller strings using Mid$



Quote:
>Personally I prefer

>Public Declare Sub CopyMemory Lib "kernel32" _
>    Alias "RtlMoveMemory" _
>(Destination As Any, Source As Any, ByVal Length As Long)

>Sub BytesToString(ByRef bytArray() As Byte, ByRef strOutput As String)
>Dim lngLen As Long
>lngLen = Ubound(bytArray) - LBound(bytArray) + 1
>strOutput = Space$(lngLen)
>CopyMemory ByVal strOutput, bytArray(LBound(bytArray)), lngLen
>End Sub

>regards

>Ian

>** invalid email address, change dk to denmark

>homepage http://www.kingsoft-denmark.com/
>Tips & Tricks page http://computers.kingsoft-denmark.com/




>> Since you're starting w/ a byte array, the prior respondent's approach is
>> best.

>> In general, however, VB does do a lot of BSTR allocation when
>concatenating
>> strings, and that can lead to performance issues.  In normal cases
>> performance is affected by 2 factors: length of concatenated strings, and
>> order of concatenation.

>> Turns out that the OLE subsystem maintains a string cache of 60K or so, so
>> concatenations that fit in this size are quick enough.  Longer strings are
>> heap-allocated and take considerably longer to effect.

>> Also related to the above, the performance of a statement such as:

>> sLongString = sLongString & " " & vbCrlf

>> can be significantly increased by specifying the concatenation order so
>that
>> small strings are concatenated first.  Like so:

>> sLongString = sLongString & (" " & vbcrlf)

>> Finally, the quickest way to concatenate strings in VB6 is to use the
>Join()
>> function.  Of course, since Join() takes an array arg, we have to deal
>with
>> dimensioning an array of adequate size in the first place.  This can be
>done
>> using repeated ReDim's (a bad idea), or Dim-ing a large array then messing
>> with its SAFEARRAY info so Join() sees only the elements you want it to.

>> The above is from Matt Curland's 'Advanced VB 6' from Addison Wesley.
>> Please refer to it for details.

>> --A



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination
I as understand it StrConv performs the same operation as RTLMoveMemory, but
there's one situation where StrConv wins hands down and that is when the
array is in a variant variable, with RTLMoveMemory you get rubbish back.

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Surely this will push ANSI bytes into an ANSI copy of a UNICODE string
> which VB will then re-convert into UNICODE

> In general StrConv looks better for this  type of operation.

> If concatenating  true strings (not byte array to string) and speed is
> critical then the trick is to pre-format the final string, and then
> insert the smaller strings using Mid$



> >Personally I prefer

> >Public Declare Sub CopyMemory Lib "kernel32" _
> >    Alias "RtlMoveMemory" _
> >(Destination As Any, Source As Any, ByVal Length As Long)

> >Sub BytesToString(ByRef bytArray() As Byte, ByRef strOutput As String)
> >Dim lngLen As Long
> >lngLen = Ubound(bytArray) - LBound(bytArray) + 1
> >strOutput = Space$(lngLen)
> >CopyMemory ByVal strOutput, bytArray(LBound(bytArray)), lngLen
> >End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination

Quote:
>I as understand it StrConv performs the same operation as RTLMoveMemory, but

Public Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" _
  (Destination As Any, _
   Source As Any, _
   ByVal Length As Long)

On the way out :
    Destination :  Unicode --> ANSI
    Source       :  Unicode --> ANSI

Then move ANSI Buff1 -->  ANSI Buff2

On the way back
    Destination :  ANSI --> Unicode
    Source       :  ANSI --> Unicode

I seriously doubt that StrConv is quite as mindblowingly inefficient

But then ... on the other hand ...



Quote:
>I as understand it StrConv performs the same operation as RTLMoveMemory, but
>there's one situation where StrConv wins hands down and that is when the
>array is in a variant variable, with RTLMoveMemory you get rubbish back.

>regards

>Ian

>** invalid email address, change dk to denmark

>homepage http://www.kingsoft-denmark.com/
>Tips & Tricks page http://tips.kingsoft-denmark.com/



>> Surely this will push ANSI bytes into an ANSI copy of a UNICODE string
>> which VB will then re-convert into UNICODE

>> In general StrConv looks better for this  type of operation.

>> If concatenating  true strings (not byte array to string) and speed is
>> critical then the trick is to pre-format the final string, and then
>> insert the smaller strings using Mid$



>> >Personally I prefer

>> >Public Declare Sub CopyMemory Lib "kernel32" _
>> >    Alias "RtlMoveMemory" _
>> >(Destination As Any, Source As Any, ByVal Length As Long)

>> >Sub BytesToString(ByRef bytArray() As Byte, ByRef strOutput As String)
>> >Dim lngLen As Long
>> >lngLen = Ubound(bytArray) - LBound(bytArray) + 1
>> >strOutput = Space$(lngLen)
>> >CopyMemory ByVal strOutput, bytArray(LBound(bytArray)), lngLen
>> >End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficient string concatination
I read that using fixed length strings doesn't cause all the BSTR
allocations. Of course, then size prediction and memory becomes a problem.

Jonathan Allen


Quote:
> Surely this will push ANSI bytes into an ANSI copy of a UNICODE string
> which VB will then re-convert into UNICODE

> In general StrConv looks better for this  type of operation.

> If concatenating  true strings (not byte array to string) and speed is
> critical then the trick is to pre-format the final string, and then
> insert the smaller strings using Mid$



> >Personally I prefer

> >Public Declare Sub CopyMemory Lib "kernel32" _
> >    Alias "RtlMoveMemory" _
> >(Destination As Any, Source As Any, ByVal Length As Long)

> >Sub BytesToString(ByRef bytArray() As Byte, ByRef strOutput As String)
> >Dim lngLen As Long
> >lngLen = Ubound(bytArray) - LBound(bytArray) + 1
> >strOutput = Space$(lngLen)
> >CopyMemory ByVal strOutput, bytArray(LBound(bytArray)), lngLen
> >End Sub

> >regards

> >Ian

> >** invalid email address, change dk to denmark

> >homepage http://www.kingsoft-denmark.com/
> >Tips & Tricks page http://computers.kingsoft-denmark.com/




> >> Since you're starting w/ a byte array, the prior respondent's approach
is
> >> best.

> >> In general, however, VB does do a lot of BSTR allocation when
> >concatenating
> >> strings, and that can lead to performance issues.  In normal cases
> >> performance is affected by 2 factors: length of concatenated strings,
and
> >> order of concatenation.

> >> Turns out that the OLE subsystem maintains a string cache of 60K or so,
so
> >> concatenations that fit in this size are quick enough.  Longer strings
are
> >> heap-allocated and take considerably longer to effect.

> >> Also related to the above, the performance of a statement such as:

> >> sLongString = sLongString & " " & vbCrlf

> >> can be significantly increased by specifying the concatenation order so
> >that
> >> small strings are concatenated first.  Like so:

> >> sLongString = sLongString & (" " & vbcrlf)

> >> Finally, the quickest way to concatenate strings in VB6 is to use the
> >Join()
> >> function.  Of course, since Join() takes an array arg, we have to deal
> >with
> >> dimensioning an array of adequate size in the first place.  This can be
> >done
> >> using repeated ReDim's (a bad idea), or Dim-ing a large array then
messing
> >> with its SAFEARRAY info so Join() sees only the elements you want it
to.

> >> The above is from Matt Curland's 'Advanced VB 6' from Addison Wesley.
> >> Please refer to it for details.

> >> --A



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. String Concatination problems

2. Concatination of strings

3. string concatination

4. aggregate string concatination

5. Efficient converting from byte-string to word-string

6. Efficient way of string manipulation

7. Efficient string concatenation

8. Efficient handling of strings from serial port device?

9. Efficient use of strings (Dim, Redim, and all that)

10. Efficient string handling

11. Most efficient way to strip leading zeros from string

12. concatination question?

 

 
Powered by phpBB® Forum Software