|
-
Nov 11th, 2005, 09:36 AM
#1
Thread Starter
Frenzied Member
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:
Dim perc As Long
For x = 0 To 50000
perc = Round((x / 50000) * 100)
fraProgress.Caption = "Progress - " & perc & "%"
Next
or
VB Code:
For x = 0 To 50000
fraProgress.Caption = "Progress - " & Round((x / 50000) * 100) & "%"
Next
I am thinking the first one would be the best, the second VB will create a Variant for to calculate, right ?
Thanks
-
Nov 11th, 2005, 09:40 AM
#2
Re: Optimization
I would go with the first one.
-
Nov 11th, 2005, 09:53 AM
#3
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.
-
Nov 11th, 2005, 10:06 AM
#4
Thread Starter
Frenzied Member
Re: Optimization
 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.
-
Nov 11th, 2005, 10:11 AM
#5
Thread Starter
Frenzied Member
Re: Optimization
It works, not much difference, but I am sure there's one.
Here's my code now:
VB Code:
Dim numOfBooks As Long, numOfBooksDivided As Long
numOfBooks = UBound(allBooks, 2)
numOfBooksDivided = numOfBooks / 100
For x = 0 To numOfBooks
perc = Round( (x + 1) / numOfBooksDivided )
fraProgress.Caption = "Progress - " & perc & "%"
Next
-
Nov 11th, 2005, 10:24 AM
#6
Re: Optimization
If you want to speed it up (and don't care about decimal precision) try this:
VB Code:
perc = (x + 1) \ numOfBooksDivided
That is integer division. I might not be what you want because you could lose some accuracy.
-
Nov 11th, 2005, 10:28 AM
#7
Thread Starter
Frenzied Member
Re: Optimization
 Originally Posted by eyeRmonkey
If you want to speed it up (and don't care about decimal precision) try this:
VB Code:
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.
-
Nov 11th, 2005, 10:35 AM
#8
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:
Private Sub Form_Load()
Dim numOfBooks As Long, numOfBooksDivided As Double
Dim perc As Double
' then some test code...
Dim allBooks() As Long, x As Long
ReDim allBooks(2, 20)
numOfBooks = UBound(allBooks, 2)
numOfBooksDivided = 100 / numOfBooks
For x = 0 To numOfBooks
Me.Caption = "Progress - " & CStr(CLng(perc)) & "%"
perc = perc + numOfBooksDivided
Next
End Sub
Boy am I efficient there!
-
Nov 11th, 2005, 10:45 AM
#9
Thread Starter
Frenzied Member
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:
Dim allBooks() As Variant
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
-
Nov 11th, 2005, 11:02 AM
#10
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.
-
Nov 11th, 2005, 11:07 AM
#11
Thread Starter
Frenzied Member
Re: Optimization
 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 ?
-
Nov 11th, 2005, 11:33 AM
#12
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:
Dim perclast As Long
' ... the other code...
' a small trick to make the "0%" appear
perclast = -1
For x = 0 To numOfBooks
If perclast < CLng(perc) Then
perclast = CLng(perc)
Me.Caption = "Progress - " & CStr(perclast) & "%"
End If
perc = perc + numOfBooksDivided
Next
And now label is updated only if it required (= 101 times instead of 50000 times).
-
Nov 11th, 2005, 11:46 AM
#13
Thread Starter
Frenzied Member
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.
-
Nov 11th, 2005, 12:07 PM
#14
Re: Optimization
I used the high-resolution performance counter to time these methods. The fastest method at 61ms is
VB Code:
'*** 61ms
Dim numOfBooks As Long, numOfBooksDivided As Long
Dim perc As Long
numOfBooks = UBound(allBooks, 2)
numOfBooksDivided = numOfBooks / 100
For x = 0 To numOfBooks
'Don't use this one, it takes 90ms
'perc = Round((x + 1) / numOfBooksDivided)
perc = (x + 1) \ numOfBooksDivided
fraProgress.Caption = "Progress - " & perc & "%"
Next
The next method took 74ms
VB Code:
'*** 74ms
Dim numOfBooks As Long, numOfBooksDivided As Double
Dim perc As Double
numOfBooks = UBound(allBooks, 2)
numOfBooksDivided = 100 / numOfBooks
For x = 0 To numOfBooks
fraProgress.Caption = "Progress - " & CStr(CLng(perc)) & "%"
perc = perc + numOfBooksDivided
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|