?
Solved

Excessive CPU usage while running VB.NET devenv.exe seems to be to probelm.

Posted on 2005-05-13
16
Medium Priority
?
505 Views
Last Modified: 2008-01-09
I am converting a VB6 app to Visual Studio 2003. I have one small section of code that takes over 30 seconds to exeute. The following is the code:
            Do Until rsGRShort.EOF
                iCount = 0
                sExitAvailable = "NO"
                Do Until iCount = lstSelect.Items.Count Or sExitAvailable = "YES"
                    sWorkBuild = VB.Left(VB6.GetItemString(lstSelect, iCount), 45)
                    iByteCount = InStr(1, sWorkBuild, " ")
                    sWorkBuild = VB.Left(sWorkBuild, iByteCount - 1)
                    If sWorkBuild = rsGRShort.Fields("DEVICE_NAME").Value Then sExitAvailable = "YES"
                    iCount = iCount + 1
                Loop
                If sExitAvailable = "NO" Then
                    sGPCDeviceName = rsGRShort.Fields("DEVICE_NAME").Value
                    lstAvailable.Items.Add(rsGRShort.Fields("DEVICE_NAME").Value)
                End If
                rsGRShort.MoveNext()
            Loop

There are only 83 rows in the record set and 83 entries in the list box.  When this routine runs devenv.exe is using over 80% of my CPU (Pent 4 - 3.2) or 30 seconds.  This seems excessive to me. Does anyone have any ideas.
0
Comment
Question by:larryH
  • 7
  • 5
  • 4
16 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 14002415
The looping of itself does not seem to account for it.  At most it is 83^2 which is <7K.  Which suggests that it is some operation within the loop.

My guess is that the lines

   sWorkBuild = VB.Left(VB6.GetItemString(lstSelect, iCount), 45)
   iByteCount = InStr(1, sWorkBuild, " ")
   sWorkBuild = VB.Left(sWorkBuild, iByteCount - 1)
   If sWorkBuild = rsGRShort.Fields("DEVICE_NAME").Value Then sExitAvailable = "YES"

are collecting an item from a listbox, taking the leftmost values up to " " from that and comparing it with a device name.  That comparison might be more succinctly handled (in code and I imagine, although I do not know for certain, in terms of operational efficiency) by

   if Instr(VB6.GetItemString(lstSelect, iCount), rsGRShort.Fields("DEVICE_NAME").Value) = 1 Then ...

But I doubt it would make that much difference.  Which leaves just the VB6.GetItemString() function as the most likely culprit.  How does that work?

Roger
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14002432
In particular, is it adding another layer of looping so that the potential total is 83^3, which is >571K?
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14003889
Another few thoughts.

1)      rsGRShort.Fields("DEVICE_NAME").Value will only change on each pass through the outer loop but you are recalculating it on each pass through the inner loop, plus twice more in the outer loop.  Internally, this means that there may be ~7K occasions on which the processor has to root through the fields to find the fieldname "DEVICE_NAME".  That could be reduced to 83 occasions if you were to declare a variable to hold that value, read the value into that variable at the start of each outer loop and replace all references by that variable.

2)      The device of having the sExitAvailable flag to exit from the inner loop is unnecessary, as is the additional conditional code after the inner loop.  The logic is simply that, if a match is found in the inner loop, the matched value is placed in sGPCDeviceName and in lstAvailable and the loop is exited.  If it is not found before the loop is exhausted the next pass of the outer loop begins without any action on those variables.

3)      The value of sGPCDeviceName keeps being written but then overwritten before it is ever used.  It is only the last value which is written to it which will survive all passes through the outer loop, so it would be more efficient to set when the outer loop has finished.

4)      It would, I think, be more efficient to use a For ... Next loop or even a For Each ... Next loop for the inner loop rather than coding your own counter.

Putting all those ideas together alternative, more efficient, code might be

            Do Until rsGRShort.EOF
                sDevName = rsGRShort.Fields("DEVICE_NAME").Value
                For iCount = 0 to lstSelect.Items.Count-1
                    If Instr(VB6.GetItemString(lstSelect, iCount), sDevName & " ") = 1 Then
                        lstAvailable.Items.Add(sDevName)
                        Exit For
                    End If
                Next
                rsGRShort.MoveNext()
            Loop
            sGPCDeviceName = lstAvailable.Items(lstAvailable.Items.Count - 1)
            'this will be the last item added (assuming the listbox is not sorted)

I still feel, however, that the real problem might lie outside this small section of code.  I have already mentioned that the only external reference seems to be to VB6.GetItemString so that it may be worth looking at that.  Besides that, it looks as though you are using DAO for data-handling rather than ADO.NET.  The documentation suggests that that is less efficient in a VB.NET setting.

Roger
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:larryH
ID: 14010686
I found the line that is causing the slowness.  It is:
                  sWorkBuild = VB.Left(VB6.GetItemString(lstSelect, iCount), 45)
If I replace this with:
                  sWorkBuild = "test line "
The code runs in five seconds, with the GetItemString it takes 45 seconds.  Any ideas??
Is there a better way to get info out of a list box in VB.NET.  ??
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14010833
try

lstSelect(icount)

Corey2
0
 
LVL 34

Accepted Solution

by:
Sancler earned 150 total points
ID: 14010852
What exactly is VB6.GetItemString(lstSelect, iCount) doing?  If it just getting the text of the item at index iCount in a listbox called lstSelect the native VB.Net code is

   sWorkBuild = lstSelect.Items(iCount).ToString

If it is something more complex than that, we'd need to know what in order to suggest an alternative.

Roger
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14010857
no, sorry

lstSelect.items(icount).tostring
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14010942
0
 

Author Comment

by:larryH
ID: 14010977
Thanks,
This was a VB6 program that was run thru the conversion program.  All I was doing was getting the contents of the list box line and you last instrunction worked great.  I wounder what it was doing myself that was taking so long. I will replace every occurance in my program thanks.
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14011099
I realize you already accepted an answer but I saw more room for improvement
try

Dim ItemIndex as Long

ItemIndex = lstSelect.FindString(rsGRShort.Fields("DEVICE_NAME").Value)

If ItemIndex = -1 then
        sGPCDeviceName = rsGRShort.Fields("DEVICE_NAME").Value
        lstAvailable.Items.Add(rsGRShort.Fields("DEVICE_NAME").Value)
end if

also for trimming spaces off of strings
VB.Trim(sWorkBuild)

would be an improvement over

 iByteCount = InStr(1, sWorkBuild, " ")
 sWorkBuild = VB.Left(sWorkBuild, iByteCount - 1)

also check out Ltrim and Rtrim

Corey2
0
 

Author Comment

by:larryH
ID: 14011432
Thanks FindString work great with less code.

0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14011446
Did it run any faster?
0
 

Author Comment

by:larryH
ID: 14011617
About the same but my code is much cleaner.

It took about 2 seconds either way

Thanks

Is there one good book you can reccommend for VB.NET Visual Basic Programming.
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14011717
Not personally.  I learn this stuff on the fly.  All I could suggest is good use of references.  I use EE and the .NET 1.1 SDK mostly.  The sdk is mostly what is available from the search at the links I pointed out earlier and can be downloaded for an internet free reference.
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 14011785
You might want to change this

if ItemIndex = -1 then

to this

if ItemIndex = Listbox.NoMatches then

Just to be certain it works properly.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistboxclassfindstringtopic.asp
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14013041
Corey2

Yes, I like the FindString approach better.  Being hyper-pernickety, given the spec implied by the original code, the search ought to be for rsGRShort.Fields("DEVICE_NAME").Value & " ", which ought to be less than or equal to 34 characters long.  FindString("ABC") will find "ABCD", etc., as well as "ABC", whereas FindString("ABC ") will not.  But I suspect those additional implied requirements were always superfluous.  For instance, sWorkBuild = VB.Left(sWorkBuild, iByteCount - 1) would have thrown an exception if iByteCount had been 0 because InStr(1, sWorkBuild, " ") found no occurrence of " ".  What matters is that it works.

larryH

I second Corey2's recommendations for sources.  I'm sure there must be good books but in my view it would be hard to beat what is available in The Visual Studio .NET Combined Help Collection and sites such as this.

Roger
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question