|
-
Aug 2nd, 2001, 04:31 AM
#1
Thread Starter
Addicted Member
Writing binary file with progress
I am at present writing binary from a custom resource to a file. However I would like to keep a status bar recording the progress of the writing. Unfortunately this code (has no progress bar):
VB Code:
Dim byteArr() As Byte
byteArr = LoadResData(101,"CUSTOM")
Open theFile$ For Binary As #1
Put #1, , byteArr
Close #1
is 100 times faster (according to GetTickCount) than this code:
VB Code:
Dim byteArr() As Byte
Dim i as Integer
byteArr = LoadResData(101,"CUSTOM")
Open theFile$ For Binary As #1
For i = 0 To Ubound(byteArr)
Put #1, , byteArr(i)
UpdateStatus Int((i+1)/(Ubound(byteArr)+1)*100) 'update progressbar
Next
Close #1
I would like to keep a progress bar on the writing, but I cannot have something which is so slow. Any ideas?
-
Aug 2nd, 2001, 04:38 AM
#2
-= B u g S l a y e r =-
did u do the timecompare without the statusbar update?
its just that 100 times seems much
-
Aug 2nd, 2001, 04:53 AM
#3
Dont refresh the progress bar every change, try maybe every 10 or 100 depends on the max value.
-
Aug 2nd, 2001, 05:31 AM
#4
Thread Starter
Addicted Member
I did not refresh it
every time - i just had an empty sub for updatestatus - the simple time for looping made it take 2016 milliseconds as opposed to 19 milliseconds for the first example.
-
Aug 2nd, 2001, 05:43 AM
#5
-= B u g S l a y e r =-
damn.. that was a big diff.
hmmm don't see how u could make the progress bar ... maybe do it the way MS often does, showing a stupid anim. letting the user know that SOMETHING is happening but we don't know how far the process have reached ... (I don't like it but...)
-
Aug 2nd, 2001, 05:56 AM
#6
Refreshing anything on screen will slow down your code
-
Aug 2nd, 2001, 06:01 AM
#7
Retired VBF Adm1nistrator
Try this :
VB Code:
Dim byteArr() As Byte
Dim i As Long, nUbound As Long, nUboundPlusOne As Long
byteArr = LoadResData(101,"CUSTOM")
nUbound = Ubound(byteArr)
nUboundPlusOne = nUbound + 1
Open theFile$ For Binary As #1
For i = 0 To nUbound
Put #1, , byteArr(i)
UpdateStatus (i + 1) \ (nUboundPlusOne) * 100
Next
Close #1
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 06:08 AM
#8
even if you update the progress bar with the same value each time it will still cause the same delay (maybe a *little* shorter)
-
Aug 2nd, 2001, 08:34 AM
#9
Thread Starter
Addicted Member
plenderj
I cannot see how that can be much faster (other than not always finding the ubound). I tested it without the updatestatus line and it was still 100 times slower. I think the looping and therefore the extra put commands make it slower
-
Aug 2nd, 2001, 08:59 AM
#10
Retired VBF Adm1nistrator
Well prestoring constants is a standard way of optimizing/speeding up code.
As is using the Long data type instead of Integer.
You could also try something like this :
VB Code:
Open theFile For Binary As #1
For i = 0 To nUbound
Put #1, , byteArr(i)
If ((i Mod 10) = 0) Then UpdateStatus((i + 1) \ (nUboundPlusOne) * 100)
Next
Close #1
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 09:04 AM
#11
Thread Starter
Addicted Member
I agree
But I ran just:
VB Code:
Dim byteArr() As Byte
Dim i As Integer
byteArr = LoadResData(101,"CUSTOM")
Open theFile$ For Binary As #1
For i = 0 To UBound(byteArr)
Put #1, , byteArr(i)
Next i
Close #1
and just that (no status update) was 100 times slower.
-
Aug 2nd, 2001, 09:34 AM
#12
Retired VBF Adm1nistrator
Well obviously iterating through the array its going to be slower than just outputting the array as is.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 09:37 AM
#13
Thread Starter
Addicted Member
Last edited by RCharlton; Aug 2nd, 2001 at 09:41 AM.
-
Aug 2nd, 2001, 09:39 AM
#14
Retired VBF Adm1nistrator
Apparantly
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 09:49 AM
#15
Retired VBF Adm1nistrator
Im getting lots of different answers, but on average this is showing me about 10 times slower....
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private time1 As Long
Private time2 As Long
Private Sub Form_Load()
sub1
sub2
Debug.Print time1 & "," & time2
End Sub
Private Sub sub1()
Dim byteArr() As Byte, i As Long
ReDim byteArr(0)
For i = 0 To 50000
byteArr(i) = CByte(i Mod 255)
ReDim Preserve byteArr(UBound(byteArr) + 1)
Next i
time1 = GetTickCount
Open "c:\myTest.txt" For Binary As #1
Put #1, , byteArr
Close #1
time1 = GetTickCount - time1
End Sub
Private Sub sub2()
Dim byteArr() As Byte, i As Long
ReDim byteArr(0)
For i = 0 To 50000
byteArr(i) = CByte(i Mod 255)
ReDim Preserve byteArr(UBound(byteArr) + 1)
Next i
time2 = GetTickCount
Open "c:\myTest.txt" For Binary As #1
For i = 0 To UBound(byteArr)
Put #1, , byteArr(i)
Next
Close #1
time2 = GetTickCount - time2
End Sub
I modified the Form_Load price as follows :
VB Code:
Private Sub Form_Load()
Dim x As Long
For x = 0 To 10
sub1
sub2
Debug.Print time1 & "," & time2
Next x
End Sub
and get the following :
Code:
0,285
0,295
0,290
5,290
0,285
0,290
5,290
0,285
0,290
5,285
0,285
Last edited by plenderj; Aug 2nd, 2001 at 09:53 AM.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 09:57 AM
#16
Thread Starter
Addicted Member
It depends on the file size though
I was writing a 440 K file and I got:
19,2124
20,2090
19,2143
19,2119
And then you would have to have the status updating...
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
|