Results 1 to 16 of 16

Thread: Writing binary file with progress

  1. #1

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    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:
    1. Dim byteArr() As Byte
    2.  
    3. byteArr = LoadResData(101,"CUSTOM")
    4.  
    5. Open theFile$ For Binary As #1
    6. Put #1, , byteArr
    7. Close #1

    is 100 times faster (according to GetTickCount) than this code:

    VB Code:
    1. Dim byteArr() As Byte
    2. Dim i as Integer
    3.  
    4. byteArr = LoadResData(101,"CUSTOM")
    5.  
    6. Open theFile$ For Binary As #1
    7. For i = 0 To Ubound(byteArr)
    8. Put #1, , byteArr(i)
    9. UpdateStatus Int((i+1)/(Ubound(byteArr)+1)*100) 'update progressbar
    10. Next
    11. Close #1

    I would like to keep a progress bar on the writing, but I cannot have something which is so slow. Any ideas?
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    did u do the timecompare without the statusbar update?

    its just that 100 times seems much
    -= a peet post =-

  3. #3
    chenko
    Guest
    Dont refresh the progress bar every change, try maybe every 10 or 100 depends on the max value.

  4. #4

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    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.
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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...)
    -= a peet post =-

  6. #6
    chenko
    Guest
    Refreshing anything on screen will slow down your code

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Try this :

    VB Code:
    1. Dim byteArr() As Byte
    2. Dim i As Long, nUbound As Long, nUboundPlusOne As Long
    3.  
    4. byteArr = LoadResData(101,"CUSTOM")
    5. nUbound = Ubound(byteArr)
    6. nUboundPlusOne = nUbound + 1
    7.  
    8. Open theFile$ For Binary As #1
    9.     For i = 0 To nUbound
    10.         Put #1, , byteArr(i)
    11.         UpdateStatus (i + 1) \ (nUboundPlusOne) * 100
    12.     Next
    13. Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    chenko
    Guest
    even if you update the progress bar with the same value each time it will still cause the same delay (maybe a *little* shorter)

  9. #9

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    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
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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:
    1. Open theFile For Binary As #1
    2.     For i = 0 To nUbound
    3.         Put #1, , byteArr(i)
    4.         If ((i Mod 10) = 0) Then UpdateStatus((i + 1) \ (nUboundPlusOne) * 100)
    5.     Next
    6. Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    I agree

    But I ran just:

    VB Code:
    1. Dim byteArr() As Byte
    2. Dim i As Integer
    3.  
    4. byteArr = LoadResData(101,"CUSTOM")
    5.  
    6. Open theFile$ For Binary As #1
    7.      For i = 0 To UBound(byteArr)
    8.           Put #1, , byteArr(i)
    9.      Next i
    10. Close #1
    and just that (no status update) was 100 times slower.
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  13. #13

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202
    100 times though?
    Last edited by RCharlton; Aug 2nd, 2001 at 09:41 AM.
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Apparantly
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Im getting lots of different answers, but on average this is showing me about 10 times slower....

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4. Private time1 As Long
    5. Private time2 As Long
    6.  
    7. Private Sub Form_Load()
    8.     sub1
    9.     sub2
    10.     Debug.Print time1 & "," & time2
    11. End Sub
    12.  
    13.  
    14. Private Sub sub1()
    15.     Dim byteArr() As Byte, i As Long
    16.     ReDim byteArr(0)
    17.  
    18.     For i = 0 To 50000
    19.         byteArr(i) = CByte(i Mod 255)
    20.         ReDim Preserve byteArr(UBound(byteArr) + 1)
    21.     Next i
    22.    
    23.     time1 = GetTickCount
    24.     Open "c:\myTest.txt" For Binary As #1
    25.         Put #1, , byteArr
    26.     Close #1
    27.     time1 = GetTickCount - time1
    28.    
    29. End Sub
    30.  
    31. Private Sub sub2()
    32.  
    33.     Dim byteArr() As Byte, i As Long
    34.     ReDim byteArr(0)
    35.    
    36.     For i = 0 To 50000
    37.         byteArr(i) = CByte(i Mod 255)
    38.         ReDim Preserve byteArr(UBound(byteArr) + 1)
    39.     Next i
    40.    
    41.     time2 = GetTickCount
    42.     Open "c:\myTest.txt" For Binary As #1
    43.         For i = 0 To UBound(byteArr)
    44.             Put #1, , byteArr(i)
    45.         Next
    46.     Close #1
    47.     time2 = GetTickCount - time2
    48.    
    49. End Sub

    I modified the Form_Load price as follows :
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim x As Long
    3.     For x = 0 To 10
    4.         sub1
    5.         sub2
    6.         Debug.Print time1 & "," & time2
    7.     Next x
    8. 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]

  16. #16

    Thread Starter
    Addicted Member RCharlton's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    202

    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...
    Richard Charlton

    VB 6.0, Java 2.0, C++, PHP, Perl, HTML, Javascript

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