vb.net, so slow !!!???

Z

zw2000

Hi,
I have read a few articles regarding the slow speed of vb.net
application, did a little experiment myself. It's a simple multiple
loop here are the code in vb.net and vb6
vb.net:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim A0 As Short
Dim A1 As Short
Dim A2 As Short
Dim A3 As Short
Dim A4 As Short
Dim A5 As Short
Dim Count As Integer = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
Count += 1
Label1.Text = Count
Label1.Refresh()
Next
Next
Next
Next
Next
Next
Label1.Text = Count
Label1.Refresh()
End Sub
vb6:
Private Sub Command1_Click()
Dim count As Long
Dim A0 As Integer
Dim A1 As Integer
Dim A2 As Integer
Dim A3 As Integer
Dim A4 As Integer
Dim A5 As Integer
count = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
count = count + 1
Label1.Caption = count
Label1.Refresh
Next
Next
Next
Next
Next
Next
Label1.Caption = count
Label1.Refresh
End Sub

The results are shocking. It only took about 3 minutes for vb6
application to finish the loop. For vb.net, didn't even finish the loop
in 15 minutes, I lost patience, close the program anyway. Don't know
how long it would actually take.
I'm just a beginner on VB, couldn't figure out why. Can anybody explain
to me, thank u.
 
C

Cor Ligthert [MVP]

ZW,

Why are you running in VB6 in its internal (int) format and are converting
everytime that internal Integer format in VBNet to short. (You should in top
of your program set Option Strict ON if you do this kind of tests, than you
see it direct).

Try it again and set than the following changes in your code.
vb.net:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim A0 As Short All shorts has to be Integer
Dim A1 As Short
Dim A2 As Short
Dim A3 As Short
Dim A4 As Short
Dim A5 As Short
Dim Count As Integer = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
Count += 1
Label1.Text = Count Label1.Text = Count.ToString
Label1.Refresh()
Next
Next
Next
Next
Next
Next
Label1.Text = Count.ToString
Label1.Refresh()
End Sub

I am currious for your result then

Cor
 
T

Tom Shelton

Hi,
I have read a few articles regarding the slow speed of vb.net
application, did a little experiment myself. It's a simple multiple
loop here are the code in vb.net and vb6
vb.net:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim A0 As Short
Dim A1 As Short
Dim A2 As Short
Dim A3 As Short
Dim A4 As Short
Dim A5 As Short
Dim Count As Integer = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
Count += 1
Label1.Text = Count
Label1.Refresh()
Next
Next
Next
Next
Next
Next
Label1.Text = Count
Label1.Refresh()
End Sub
vb6:
Private Sub Command1_Click()
Dim count As Long
Dim A0 As Integer
Dim A1 As Integer
Dim A2 As Integer
Dim A3 As Integer
Dim A4 As Integer
Dim A5 As Integer
count = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
count = count + 1
Label1.Caption = count
Label1.Refresh
Next
Next
Next
Next
Next
Next
Label1.Caption = count
Label1.Refresh
End Sub

The results are shocking. It only took about 3 minutes for vb6
application to finish the loop. For vb.net, didn't even finish the loop
in 15 minutes, I lost patience, close the program anyway. Don't know
how long it would actually take.
I'm just a beginner on VB, couldn't figure out why. Can anybody explain
to me, thank u.

While, as Cor pointed out, you have type conversion issues in your code
that will definately cause some performance loss - the main problem is
the UI update. If you take out the lable update in the loop, the loop
will complete almost immediately. It's a fact, that VB.NET is slower
when it comes to UI operations. If you needed this is in production
code, I would be inclined to not update ever iteration of the loop.
 
H

Herfried K. Wagner [MVP]

vb.net:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim A0 As Short
Dim A1 As Short
Dim A2 As Short
Dim A3 As Short
Dim A4 As Short
Dim A5 As Short
Dim Count As Integer = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
Count += 1
Label1.Text = Count
Label1.Refresh()
Next
Next
Next
Next
Next
Next
Label1.Text = Count
Label1.Refresh()
End Sub
vb6:
Private Sub Command1_Click()
Dim count As Long
Dim A0 As Integer
Dim A1 As Integer
Dim A2 As Integer
Dim A3 As Integer
Dim A4 As Integer
Dim A5 As Integer
count = 0
For A0 = 1 To 45
For A1 = A0 + 1 To 46
For A2 = A1 + 1 To 47
For A3 = A2 + 1 To 48
For A4 = A3 + 1 To 49
For A5 = A4 + 1 To 50
count = count + 1
Label1.Caption = count
Label1.Refresh
Next
Next
Next
Next
Next
Next
Label1.Caption = count
Label1.Refresh
End Sub

The results are shocking. It only took about 3 minutes for vb6
application to finish the loop. For vb.net, didn't even finish the loop
in 15 minutes, I lost patience, close the program anyway. Don't know
how long it would actually take.

First, I suggest to replace 'As Short' with 'As Integer' in the VB.NET
version. In addition note that the JIT compiler will have to create a
native code version of the method prior to executing it the first time,
which will take some microseconds too.
 
J

Jay B. Harlow [MVP - Outlook]

zw2000,
Congratulations! you have successfully demonstrated that marshaling from
managed code (.NET) to unmanaged code takes time.

As Tom pointed out, all your time is spent in the UI update:

| Label1.Text = Count
| Label1.Refresh()

Removing those two statements from the middle of your loop causes the loop
to finish "in a matter of seconds". Because updating the UI takes time in
both VB6 & more so in .NET I normally try to be a little more friendly when
updating the UI, such as relying on the windows Paint event, or only
updating status's every n iterations...

Remember that a number of framework controls, such as Label are based on
Win32 controls. When you set properties (.Text) or call methods (.Refresh)
the CLR needs to marshal the data from the .NET managed world to the Win32
unmanaged world.


Changing the variables from Short to Integer has a slight impact but its
barely perceivable here. I would recommend using Integer here more because
Short is not offering any real benefit here. Not because there is a fraction
of a millisecond speed improvement... Besides the CLR actually uses Int32
for the calculation any way then VB converts them back to Int16 to store the
value... I would consider using Short (Int16) more where size was a
consideration, such as interop code, I really need to limit the range of the
values, or I was storing a large number of them such as an array...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi,
| I have read a few articles regarding the slow speed of vb.net
| application, did a little experiment myself. It's a simple multiple
| loop here are the code in vb.net and vb6
| vb.net:
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim A0 As Short
| Dim A1 As Short
| Dim A2 As Short
| Dim A3 As Short
| Dim A4 As Short
| Dim A5 As Short
| Dim Count As Integer = 0
| For A0 = 1 To 45
| For A1 = A0 + 1 To 46
| For A2 = A1 + 1 To 47
| For A3 = A2 + 1 To 48
| For A4 = A3 + 1 To 49
| For A5 = A4 + 1 To 50
| Count += 1
| Label1.Text = Count
| Label1.Refresh()
| Next
| Next
| Next
| Next
| Next
| Next
| Label1.Text = Count
| Label1.Refresh()
| End Sub
| vb6:
| Private Sub Command1_Click()
| Dim count As Long
| Dim A0 As Integer
| Dim A1 As Integer
| Dim A2 As Integer
| Dim A3 As Integer
| Dim A4 As Integer
| Dim A5 As Integer
| count = 0
| For A0 = 1 To 45
| For A1 = A0 + 1 To 46
| For A2 = A1 + 1 To 47
| For A3 = A2 + 1 To 48
| For A4 = A3 + 1 To 49
| For A5 = A4 + 1 To 50
| count = count + 1
| Label1.Caption = count
| Label1.Refresh
| Next
| Next
| Next
| Next
| Next
| Next
| Label1.Caption = count
| Label1.Refresh
| End Sub
|
| The results are shocking. It only took about 3 minutes for vb6
| application to finish the loop. For vb.net, didn't even finish the loop
| in 15 minutes, I lost patience, close the program anyway. Don't know
| how long it would actually take.
| I'm just a beginner on VB, couldn't figure out why. Can anybody explain
| to me, thank u.
|
 
Z

zw2000

thanks all for the reply, I change the codes as all of you said, now
both almost finish instantly. I learn something new everyday.
 
A

alcurb

thanks all for the reply, I change the codes as all of you said, now
both almost finish instantly. I learn something new everyday.


Have they really addressed your question? I mean, I'm left with one:
Why is VB6 a lot faster then VB.net using the same code?

zw2000, have you run another test after the suggested modifications to
your benchmark code? I'm curious as to which VB will outperform the
other.
 
P

Patrice

See Tom's reply : this is the UI update that is the main culprit (likely
because of interop), the simple solution he mentioned is to avoid updating
too much frequently the UI).
 
C

Cor Ligthert [MVP]

Alcurb,
zw2000, have you run another test after the suggested modifications to
your benchmark code? I'm curious as to which VB will outperform the
other.

This is told in this thread by Tom and by Jay. One of those things is by
refreshing the contents of controls with the goal to do it in nanoseconds.
As you probably know is that not readable for humans, therefore culprit to
take attention to that or better taking a lot of performance time trying
doing that (painting is the most important performance eater).

Cor
 
J

Jay B. Harlow [MVP - Outlook]

| Have they really addressed your question? I mean, I'm left with one:
| Why is VB6 a lot faster then VB.net using the same code?
VB6 runs as native code, there is no marshalling happening.

As I stated earlier VB.NET runs as managed code, it needs to marshal
arguments from the managed code to the unmanaged code.

By "marshalling" code I mean the CLR (the runtime) looks at the managed
(.NET) stack frame, copies & converts arguments to the Win32 stack frame,
then calls the Win32 version of the function. When the Win32 version is
done, the CLR then copies & converts any return values back to the managed
stack frame (from the Win32 stack frame)...

zw2000 was making 2 calls per iteration that would require marshaling in
their sample.

There are a plethora of articles on the subject of Marshalling. This may be
a good start:

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

At the very least it describes a tool that may help identify bottle necks in
your managed code. Although the "COM Interop and Platform Invoke" section
provides some good quick info on Interop & Marshalling...



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|
| (e-mail address removed) wrote:
| > thanks all for the reply, I change the codes as all of you said, now
| > both almost finish instantly. I learn something new everyday.
|
|
| Have they really addressed your question? I mean, I'm left with one:
| Why is VB6 a lot faster then VB.net using the same code?
|
| zw2000, have you run another test after the suggested modifications to
| your benchmark code? I'm curious as to which VB will outperform the
| other.
|
 
H

Herfried K. Wagner [MVP]

Jay B. Harlow said:
| Have they really addressed your question? I mean, I'm left with one:
| Why is VB6 a lot faster then VB.net using the same code?
VB6 runs as native code, there is no marshalling happening.

I think this is only partly true. While VB6 supports Unicode strings, it
uses the ANSI versions of the API functions and thus marshalling from UTF-16
to the system's ANSI codepage must be performed.
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
Yes for String versions of API's on platforms that are Ansi only (Win95)
that's probably true.

Although I'm not sure if on "NT" platforms VB6 is calling the Ansi version
of the API directly or calling the respective Unicode or Ansi version based
on the OS its on. As it would be horrible inefficient to convert the string
to Ansi call the Ansi version, then have the Ansi version convert it back to
call the Unicode version on NT/XP/Windows Server platforms where the OS is
running Unicode natively...


My understanding is that VB6 has the equivalent of:

http://www.microsoft.com/downloads/...D7-ED06-4F0D-80A4-2A7EEAEE17E2&displaylang=en

That is it runs Unicode & effectively calls the Unicode API on "NT"
platforms, on Windows 95, 98, & ME it effectively calls a helper function
that then converts the strings to call the ANSI API.


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| >| Have they really addressed your question? I mean, I'm left with one:
| > | Why is VB6 a lot faster then VB.net using the same code?
| > VB6 runs as native code, there is no marshalling happening.
|
| I think this is only partly true. While VB6 supports Unicode strings, it
| uses the ANSI versions of the API functions and thus marshalling from
UTF-16
| to the system's ANSI codepage must be performed.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 

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

Top