Speed inprovement

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

I am building an application that need high speed of execution. There
is a lot of string parsing that I do in VB.NET.

Do I gain in speed if all that string parsing is made in a dll coded in
C or C++ and added to my VB.NET application?

I would also gain the advantage of portability, but my main concern is
speed of execution.

Thank you!

Marty
 
I am building an application that need high speed of execution. There is
a lot of string parsing that I do in VB.NET.

Do I gain in speed if all that string parsing is made in a dll coded in C
or C++ and added to my VB.NET application?

I would also gain the advantage of portability, but my main concern is
speed of execution.

Can be, however why would it?

There are very fast classes for string handling in VBNet. But there can
always situations which cannot be handled.

Cor
 
Hi Cor,

Here is the idea of what I want to do:

Here is a description of the strings I have to parse. Approx 100
different string of approx 60 to 100 bytes long.

Each string contain concatened fields. Some are non formatted numbers,
date, time, and many others specific fields.

There is no way to split those string and get a easy to read array. For
each string I have to use mid$ and get each separate fields and format
it in the way it should be. Some fields need basic mathematic
calculation to have a human readable number.

At this point I was thinking to make the C dll for reading purpose, I
mean, pass the raw string to the dll, and it return a parsed array of
all fields (of data type string).

In this VB.NET code I use the string.substring() function to to get
parts of string. Is there a better VB.NET handling for that?

Do you think I could gain speed using a C/C++ dll for this situation?

Thank you for your reply.
Marty
 
Marty,

Some things I know

When you can do a character operation let that go before a string operation.

A problem is that there is no consequenty where a Microsoft.Visual Basic
method is quicker than a System method. It differs, the Instr is quicker
than the IndexOf (with strings) however there are others who are *much*
slower.

The problem is that mixing those up is confusing because of the use of the
character indexer which start in System at Zero and on MVB with the First
(1).

The regular expression is almost always (very) slow however when you can do
a lot in one search than it can become an advantage.

Never use in .Net whatever language you use things as:
dim str as string = "I" & " am" & " Cor"
or even worse
dim str as string = "It is " + "ToDay " + "march " + "1" + "8"
For that is the stringbuilder and than do it even with smallest parts.
Every string that has the slightest change will me made new.
(A string is immutable).

When you can put your strings as stringbuilder parts in a arraylist, which
is fast as well, than I assume that you can get your highest performance.

You see that there are a lot of possibilities which you don't get with a raw
language as C where you do it yourself. My opinion is that you will loose
that.

However I can be wrong of course, it is just what I assume.

I hope this helps

Cor
 
Not so long ago i had the folowing task at my job


Write a program that can read a MYSQL dump file of mutiple gigabytes
( the tests were between 2 and 7 gb ) and that wil on the fly convert the
MYSQL data types to SQL server datatypes
and import the data to MS SQL server 2000

well before i used the stringbuilder this took hours ,,,,, after using the
string builder it took minutes

I am really impressed about the string and IO handling of .Net and know that
if you use it at the right way it will be as fast as using a low level
language in a verry very optimized way

M. Posseth
 
With only 100 strings of 60 bytes long, it really doesn't matter as the speed
will be blazingly fast so long as you use stringbuilder instead of the + and
& operators. However, even with them, you applicaiton is light and speed
won't be a problem. I would suggest you might want to convert the strings
into char arrays as this might be faster and easier than using mid. You can
check each character in a loop and use stringbuilder to construct your
substrings.
 
Thanks for the info. I did test to compare my use of the string, and it
get faster with "&" than stringbuilder. But my string process is not as
heavy as yours.

Regards,
Marty
 
Marty,
Thanks for the info. I did test to compare my use of the string, and it
get faster with "&" than stringbuilder. But my string process is not as
heavy as yours.

Can you show that code, because I think that most of the ones who are
regular visit this newsgroup will see that.

I that with two constants you can get an almost equal speed.

Cor
 
Hi Cor,

The stringbuilder is definitely faster for big string concatenation, but
in this example, for each iteration I want to create a stringbuilder
object and concat small amout of string (in Button2 handling).

If I was using only one stringbuilder object and doing
mySB.Append("...") for each iteration, then stringbuilder is the fastest.

What I wanted to do is generate thousands of string as fast as possible.
Is there something I missed with stringbuilder?

Regards,
Martin

'Concat with "&"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim intRepeat As Int32
Dim i As Int32
Dim j As Int32
intRepeat = Convert.ToInt32(tbxRepeat.Text)
tbxStart.Text = Now.Second & ":" & Now.Millisecond
For j = 1 To intRepeat
Dim strTest As String
strTest = vbNullString
strTest &= Chr(2) & _
CStr(j) & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla" & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla" & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla"
Next
tbxStop.Text = Now.Second & ":" & Now.Millisecond
End Sub

'Concat with stringbuilder
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim intRepeat As Int32
Dim j As Int32
intRepeat = Convert.ToInt32(tbxRepeat.Text)
tbxStart.Text = Now.Second & ":" & Now.Millisecond
Dim strTest As String
For j = 1 To intRepeat
Dim sb As New System.Text.StringBuilder
sb.Append(Chr(2))
sb.Append(CStr(j))
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
strTest = sb.ToString
sb = Nothing
Next
tbxStop.Text = Now.Second & ":" & Now.Millisecond
End Sub
 
Marty,

You are right, with such short strings and constants it is almost the same.

(I have seen this behaviour earlier, it seems that in this case a string is
constructed and than created as string)

Cor
 
Marty,

For that you think that I did not check it. In this way you will see an
enormous time difference.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim intRepeat As Integer = CInt(tbxRepeat.Text)
Dim start As Long = Now.Ticks
Dim strTest As String = ""
For i As Integer = 1 To intRepeat
strTest &= Chr(2) & _
CStr(i) & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla" & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla" & _
"999999999" & _
"92.31" & _
"31.00" & _
"blablabla"
Next
txtConCat.Text = (Now.Ticks - start).ToString
End Sub

'Concat with stringbuilder
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim intRepeat As Integer = CInt(tbxRepeat.Text)
Dim start As Long = Now.Ticks
Dim sb As New System.Text.StringBuilder
For i As Integer = 1 To intRepeat
sb.Append(Chr(2))
sb.Append(CStr(i))
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
sb.Append("999999999")
sb.Append("92.31")
sb.Append("31.00")
sb.Append("blablabla")
Next
txtStringBuilder.Text = (Now.Ticks - start).ToString
End Sub


Cor
 
Hi Cor,

You are right, I tried it before, it is pretty fast. It is just that I
need a new string for each iteration. I don't use it as it is, my
algorithm (in small scale) look like a "for", I have to create a
stringbuilder each time, otherwise, I could use a clear method if it was
implemented in the stringbuilder, it could save time instead of doing a
new every time.

Regards,
Marty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top