Poor Performance in Visual Studio

Hi,

I am an experienced VB6 programmer, but just a new starter in Visual Studio 2005.

I have found Visual Studio 2005 to be very slow. I set up 3 simple tests with the following results:-

1. Scanning 20,000 directory entries and

retrieving date/time and size info

VB6


3

Secs

Visual Studio (Debug)13

secs

Visual Studio (exe) 3

Secs

2. loading 100,000 Nodes

into a treeview

VB6


2 secs

Visual Studio (Debug)320

secs

Visual Studio (exe)140

secs

3. loading 100,000

Itemsinto a Listview

VB62 secs 11 secs

Visual Studio (Debug)270

Secs

Visual Studio (exe)200

secs

I realise these are not something you do every day, but performance hits of up to 100 is pretty high.

I am not sure whether this is just a

feature of Visual Studio, whether my coding is poor, whether I

have inadvertantly set some funny debug options, or whether my PC is just too old to handle it.

Any comments on other experiences, or where I should go to find some info?

(Apologies if this is a dumb question, or if I have posted it in the wrong group)

Alan Cuthbertson

[1286 byte] By [AlanCuthbertson] at [2007-12-24]
# 1

Alan,

To give you a reasonable answer you will need to post the sample code you used for your tests so we can point out any errors. But also have a look at this thread where a number of compiler options are pointed out that can improve performance: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=732466&SiteID=1

Regards,

William Bartholomew

WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Also the figures of loading 100,000 nodes into a treeview or items in a listview may be a performance limitation of the windows forms controls and not VB Language per se.

I would definately show the code you are using for populating these controls as I find it strange that there should be such a large difference in runtime performance. Also, if you repeat the tests a number of times are these times actually representative of the average times.

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Thanks for the feed back.

I tried turning of the Overflow checking, but no change.

Tried running the tests several times and the results were all consistent

I did some fiddling and was able to get the Treeview load

working better (3 secs in VB, 10 secs in Visual Studio (compiled) , 43 secs in

Visual Studio IDE.

Also found I could speed up the Listview ADD command

significantly by removing the line that added a SubItem. (6 secs for VB6, 10

secs Visual Studio (compiled), 20 secs Visual Studio IDE. Presumably there is a

more efficient way of doing this?

itemx = ListView1.Items.Add("AAA")

itemx.SubItems.Insert(1, New

System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "BBBB"))

As a general question, do you normally find the IDE takes

twice as long as compiled? VB6 always seemed about the same speed,

Here is the code I used for adding the Listview:-

VB6

Private Sub Command3_Click()


Dim itemx As ListItem

Dim I As Long

Dim TimeStore As Double


ListView1.ListItems.Clear


TimeStore = Now


For I = 1 To 100000

Set itemx = ListView1.ListItems.Add

itemx.Text = "AAA"

itemx.SubItems(1) = "BBB"

Next I

Screen.MousePointer = 0


MsgBox "Start:" + Format$(TimeStore, "HH:MM:SS") + Chr$(13) + "Finish:" + Format$(Now, "HH:MM:SS")


End Sub

Visual Studio

Private Sub Command3_Click(ByVal eventSender As

System.Object, ByVal eventArgs As System.EventArgs) Handles

Command3.Click


Dim itemx As System.Windows.Forms.ListViewItem

Dim I As Long

Dim TimeStore As Double


ListView1.Items.Clear()


TimeStore = Now.ToOADate


For I = 1 To 100000

itemx = ListView1.Items.Add("AAA")


itemx.SubItems.Insert(1, New

System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "BBBB"))

Next I

MsgBox("Start:" &

VB6.Format(TimeStore, "HH:MM:SS") & Chr(13) & "Finish:" &

VB6.Format(Now, "HH:MM:SS"))


End Sub

I

reran the test and got 6 secs for VB6, 264 secs for Visual Studio in the IDE,

230 secs when compiled.

I

then tried commenting out the Line:-

itemx.SubItems.Insert(1,

New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "BBBB")),

And

run tme dropped to 20 secs in the IDE, 10 secs when Compiled. BIG IMPROVEMENT,

but not perfect, so is there a better way to add a subitem? (as I said, I am a

VB6 programmer and relied on the Conversion wizard to generate the code)

Code

for the TreeView Add (adds 10

nodes, each node has 6 children, extending down a depth of 6. Close to 100,000

nodes):-

VB6

Private Sub Command2_Click()

Dim I As Long

Dim Basenode As Node

Dim Newnode As Node


Dim TimeStore As Double


TreeView1.Nodes.Clear


TimeStore = Now


Set Basenode = TreeView1.Nodes.Add()

Basenode.Text = "root"

Basenode.Expanded = True

For I = 1 To 10

Set Newnode = TreeView1.Nodes.Add(Basenode, tvwChild)


Newnode.Text = "AAA"

Call AddBranch(Newnode, 1)

Next I


MsgBox "Start:" + Format$(TimeStore, "HH:MM:SS") + Chr$(13) + "Finish:" + Format$(Now, "HH:MM:SS")


End Sub
Sub AddBranch(Node As Node, depth)


Dim I As Integer

Dim Newnode As Node


If depth > 6 Then

Exit Sub

End If


Set Newnode = TreeView1.Nodes.Add(Node, tvwChild)


Newnode.Text = "BBB"


For I = 1 To 6

Call AddBranch(Newnode, depth + 1)

Next I


End Sub

Visual Studio

Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click


Dim I As Long

Dim Basenode As System.Windows.Forms.TreeNode

Dim Newnode As System.Windows.Forms.TreeNode

Dim TimeStore As Double

TreeView1.Nodes.Clear()


TimeStore = Now.ToOADate


Basenode = TreeView1.Nodes.Add("")

Basenode.Text = "root"

Basenode.Expand()

For I = 1 To 10

Newnode = Basenode.Nodes.Add("")

Newnode.Text = "AAA"

Call AddBranch(Newnode, 1)

Next I

System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

MsgBox("Start:" &

VB6.Format(TimeStore, "HH:MM:SS") & Chr(13) & "Finish:" &

VB6.Format(Now, "HH:MM:SS"))


End Sub


Sub AddBranch(ByRef Node As System.Windows.Forms.TreeNode, ByRef depth As Long)

Dim I As Short

Dim Newnode As System.Windows.Forms.TreeNode

If depth > 6 Then

Exit Sub

End If

Newnode = Node.Nodes.Add("BBB")

For I = 1 To 6

Call AddBranch(Newnode, depth + 1)

Next I

End Sub

AlanCuthbertson at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hi there,

There is a difference between "IDE" mode and "compile" mode. If you are using the Visual Basic Profile, essentially IDE mode is "debug" mode, and "compile" mode is retail. In the Visual Basic Profile there is no way you can modify that manually. If you change VS profile to "General" then you can setup different build configurations.

The performance problem in your code is that the .NET version of the listview (and treeview, although I did not test that) is slow adding items one at a time. Instead, create all your items, and then batch add them, like below. When I ran through the code you had, it took more then 300 seconds (my machine is slower then yours!) but with the code I pasted below, it is about 6 seconds as well. Give is a shot! Let me know if it does not work.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim I As Long

Dim TimeStore As DateTime

ListView1.Items.Clear()

TimeStore = DateTime.Now

Dim x() As ListViewItem = New ListViewItem(10000) {}

For I = 0 To 10000

x(I) = New ListViewItem("AAAA")

x(I).SubItems.Insert(1, New ListViewItem.ListViewSubItem(Nothing, "BBBB"))

Next

ListView1.Items.AddRange(x)

MsgBox("Start:" & TimeStore.ToString & Chr(13) & "Finish:" & DateTime.Now.ToString)

End Sub

TimothyNgMSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Brilliant! That works like a dream!

The only thing is, that I would never have thought of it myself. Would

this be common knowledge to anyone who knew what they were doing in

Visual Studio (i.e had been on a course) , or do you have to be a guru?

I also found out in playing that using

Newnode = Node.Nodes.Add("BBB")

is far quicker than

Newnode = Node.Nodes.Add("")

Newnode.text = "BBB"

especially when there are lots of nodes

Once again is this obvious to those experienced in Visual Studio? Is it doing a search of the nodes or something?

Also tried the Range suggestion for Nodes:-

Dim x() As TreeNode = New TreeNode(5) {}

For I = 0 To 5

x(I) = New TreeNode("AAAA")

Next I

Node.Nodes.AddRange(x)

For I = 0 To 5

Call AddBranch(x(I), depth + 1)

Next I

but that even ran slower...

AlanCuthbertson at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

To be honest, I was just experimenting with it. I know that most controls have a single Add method and a AddRange method, and in general, adding multiple objects at once should be quicker. But as you noted above, this may not hold for all controls, such as the tree control.

For the tree node, it might be faster if you build the entire node structure before you call AddRange. That is, build the sub nodes before you add to the tree.

I would say that you collect these experiences as you learn more, but unfortunately, there isn't really a good place where all this "programmer intuition" is collected and easily searchable.

TimothyNgMSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

This article covered that technique and some others:

Practical Tips For Boosting The Performance Of Windows Forms Apps

http://msdn.microsoft.com/msdnmag/issues/06/03/WindowsFormsPerformance/

CarlDichter at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...