Results 1 to 14 of 14

Thread: Optimization

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Optimization

    Hey,

    I am running a large loop and so far I cut down a lot of the processing by optimizing little things that made a huge difference.

    But I need to know some more, which between the two will take less time/memory:

    VB Code:
    1. Dim perc As Long
    2.  
    3. For x = 0 To 50000
    4.   perc = Round((x / 50000) * 100)
    5.   fraProgress.Caption = "Progress - " & perc & "%"
    6. Next
    or

    VB Code:
    1. For x = 0 To 50000
    2.   fraProgress.Caption = "Progress - " & Round((x / 50000) * 100) & "%"
    3. Next

    I am thinking the first one would be the best, the second VB will create a Variant for to calculate, right ?

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Optimization

    I would go with the first one.

  3. #3
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Optimization

    Would it be better to replace "(x/50000) * 100" with "x/500"?
    This world is not my home. I'm just passing through.

  4. #4

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    Quote Originally Posted by trisuglow
    Would it be better to replace "(x/50000) * 100" with "x/500"?
    Hmmm, I had replaced my variable that hold the number with 50000, but I guess if I would create another variable that hold myNumber / 100, then I could do:

    x / myNewVariable

    Interesting, I will try that right away.

  5. #5

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    It works, not much difference, but I am sure there's one.

    Here's my code now:

    VB Code:
    1. Dim numOfBooks As Long, numOfBooksDivided As Long
    2.  
    3. numOfBooks = UBound(allBooks, 2)
    4. numOfBooksDivided = numOfBooks / 100
    5.  
    6. For x = 0 To numOfBooks
    7.     perc = Round( (x + 1) / numOfBooksDivided )
    8.     fraProgress.Caption = "Progress - " & perc & "%"
    9. Next

  6. #6
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Optimization

    If you want to speed it up (and don't care about decimal precision) try this:

    VB Code:
    1. perc = (x + 1) \ numOfBooksDivided

    That is integer division. I might not be what you want because you could lose some accuracy.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    Quote Originally Posted by eyeRmonkey
    If you want to speed it up (and don't care about decimal precision) try this:

    VB Code:
    1. perc = (x + 1) \ numOfBooksDivided

    That is integer division. I might not be what you want because you could lose some accuracy.
    Well I rounded it off anyway, so thanks, it works for this purpose.

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimization

    Uhhuh, with my test the code doesn't even run. The code wouldn't work with less than a hundred books, because you get a divizion by zero error. This is because you use Long datatype instead of Single or Double, which can hold decimals.

    Now, to actually make things fast and smooth:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim numOfBooks As Long, numOfBooksDivided As Double
    3.     Dim perc As Double
    4.     ' then some test code...
    5.     Dim allBooks() As Long, x As Long
    6.     ReDim allBooks(2, 20)
    7.    
    8.     numOfBooks = UBound(allBooks, 2)
    9.     numOfBooksDivided = 100 / numOfBooks
    10.    
    11.     For x = 0 To numOfBooks
    12.         Me.Caption = "Progress - " & CStr(CLng(perc)) & "%"
    13.         perc = perc + numOfBooksDivided
    14.     Next
    15. End Sub

    Boy am I efficient there!

  9. #9

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    Hehe, that is because I did not include all my code, the allBooks are a returned of recordset, so let say

    VB Code:
    1. Dim allBooks() As Variant
    2. ReDim allBooks(2, 50000)

    And I do not need decimal, your code show 1.23232323123123%, which is not what I need, I do not need decimal. But I like you idea though

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimization

    Did you include the CStr(CLng()) conversions? It first converts to Long and then to String (avoiding automatical datatype coercion, which causes slowdown). A Long datatype can't hold a decimal.

  11. #11

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    Quote Originally Posted by Merri
    Did you include the CStr(CLng()) conversions? It first converts to Long and then to String (avoiding automatical datatype coercion, which causes slowdown). A Long datatype can't hold a decimal.
    No, but but wouldn't two conversion take longer than just perc = (x + 1) \ numOfBooksDivided ?

  12. #12
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimization

    The other conversion would be made automatically through datatype coercion if it wasn't there (being slightly slower). I admit using Double datatype is slower than a Long (which is the fastest with 32-bit processor), but if we really take a closer look into your code, you are using Strings, which cause far more slowdown than anything else in the code. So the only advantage to my code is that it is less error prone (no divizion by zero errors, unless there is only one book; but that is easy to check for), it is far more accurate with lesser book amounts and it is still very lightweight (only two mathematical calculations per loop and a conversion for progress display).


    So, the next step to increase performance would be to make the label update only if it is changed:

    VB Code:
    1. Dim perclast As Long
    2.  
    3.     ' ... the other code...
    4.  
    5.     ' a small trick to make the "0%" appear
    6.     perclast = -1
    7.     For x = 0 To numOfBooks
    8.         If perclast < CLng(perc) Then
    9.             perclast = CLng(perc)
    10.             Me.Caption = "Progress - " & CStr(perclast) & "%"
    11.         End If
    12.         perc = perc + numOfBooksDivided
    13.     Next

    And now label is updated only if it required (= 101 times instead of 50000 times).

  13. #13

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Optimization

    Yeah, the label update makes a lot of sense, thanks.

    It made me think of another thing I was doing wrong, I also have a progress bar and I was setting the max to the numOfBooks and was incrementing it 50000 times, instead I set the max to 100 and will increment with perc only if it changed

    Thanks again.

  14. #14
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Optimization

    I used the high-resolution performance counter to time these methods. The fastest method at 61ms is
    VB Code:
    1. '*** 61ms
    2. Dim numOfBooks As Long, numOfBooksDivided As Long
    3. Dim perc As Long
    4.  
    5. numOfBooks = UBound(allBooks, 2)
    6. numOfBooksDivided = numOfBooks / 100
    7.  
    8. For x = 0 To numOfBooks
    9.     'Don't use this one, it takes 90ms
    10.     'perc = Round((x + 1) / numOfBooksDivided)
    11.     perc = (x + 1) \ numOfBooksDivided
    12.     fraProgress.Caption = "Progress - " & perc & "%"
    13. Next
    The next method took 74ms
    VB Code:
    1. '*** 74ms
    2. Dim numOfBooks As Long, numOfBooksDivided As Double
    3.     Dim perc As Double
    4.  
    5.     numOfBooks = UBound(allBooks, 2)
    6.     numOfBooksDivided = 100 / numOfBooks
    7.  
    8.     For x = 0 To numOfBooks
    9.         fraProgress.Caption = "Progress - " & CStr(CLng(perc)) & "%"
    10.         perc = perc + numOfBooksDivided
    11.     Next
    Here is a post on how to use the high-resolution performance counter
    http://www.vbforums.com/showthread.p...18#post2033918

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width