Results 1 to 38 of 38

Thread: Paper on Efficient File I/O in VB

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Paper on Efficient File I/O in VB

    Here's something I put together last night and modified a bit this morning. Basically this is, in my opinion, the best way of doing file I/O in VB using native VB functions.

    My code is the third method.
    Its fast, but its also in a loop, so that the application won't look crashed.

    Let me know what you think ...
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    impressive!

    CopyMemory is superfast

    i got:
    1) = 3910 (Normal Input)
    2) = 4351 (Normal Do...Loop)
    3) = 3799 (Yours)

    which means that in my case, your method was quicker than BOTH!
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yes it is quite cool isn't it
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i'm guessing that the usual functions, and string appending basically copies memory accross at some point (it would have to!), but your method skips the error checking etc. so its quicker.

    i wonder what the downsides are?

    if its dangerous to use?

    If it's not then i'll use it all the time

    be worth finding out...?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237
    Whjy not just use Get?

    Code times :
    Input(Lof(), #filenum) 5919
    Normal Do...Loop 7721
    Modified Do...Loop 5628
    Get: 180

    Buffer sizes : 438899,438899,438899,438899
    Buffers equal : True
    Another light-hearted post from Guru

  6. #6

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Post your code ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    even if it is faster with Get, using CopyMemory just for the String appending bit is faster than str1 = str1 & str2

    and we all can use that!
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    there are lots of ways to speed up your apps when it comes to string manipulation though.

    check this out...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  9. #9
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ignore prev thread and dont download, i got me no.s mixed up.

    DAMN it, i feel silly now
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  10. #10
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237
    Originally posted by plenderj
    Post your code ?
    apend it to your code
    Code:
    dim time4 as long
    dim strBuff5 as string
    
        Debug.Print "6/5) Now using get"
        time4 = GetTickCount
        Open myFile For Binary As #1
            strBuff5 = Space(LOF(1))
            Get #1, , strBuff5
        Close #1
        time4 = GetTickCount - time4
        
        
        
    '' Display results, and confirm that resulting file buffers are equal
    ''
        Debug.Print "Code times : " & vbCrLf & _
                "  Input(Lof(), #filenum)" & vbTab & time1 & vbCrLf & _
                "  Normal Do...Loop    " & vbTab & time2 & vbCrLf & _
                "  Modified Do...Loop  " & vbTab & time3 & vbCrLf & _
                "  Get: " & vbTab & time4 & vbCrLf & vbCrLf & _
                "  Buffer sizes  :     " & vbTab & Len(strBuff) & "," & Len(strBuff2) & "," & Len(strBuff4) & "," & Len(strBuff5) & vbCrLf & _
                "  Buffers equal :     " & vbTab & ((strBuff2 = strBuff4) And (strBuff = strBuff2))
    Another light-hearted post from Guru

  11. #11
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    using Get took 20 milliseconds on mine, you sure thats right!?!?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  12. #12

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hmm you're right!
    It pisses all over my code.
    I had never considered using Get() to input the data...

    Nice one
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    heres something interesting...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  14. #14
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Jamie, why not start a VB Optimisations thread in Chit Chat?

    plonk your code in it too. i'll add a few ideas.

    people will argue about who's code is faster that who's etc, and add their own. everyone will learn...

    very
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  15. #15

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Why chit-chat ?
    We get post count increases if we do it here
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    but wont we get slapped by the admins etc for that?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  17. #17

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    oooo a slapping. I like the idea of that
    Heh, have you seen the Sentience thread in the Games&Graphics Programming forum ?
    About 35 pages full of ****e
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  18. #18
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    aha, the plot thickens heheheh
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  19. #19
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Naw, as long as it's VB related and relatively useful then it's fine.

    Oi, Irish! Watch it!

    35 pages of shiite? I'll kick yo ass - that thread's essential to Sentience!
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  20. #20

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yeah but man you've got to admit it, there's a serious amount of crap in there too
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    <chant> fight, fight, fight...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  22. #22
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Most of it posted by you, ya barmy Mick!
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  23. #23

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I beg to differ !
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  24. #24
    RobIII
    Guest
    As mentioned before: GET is REALLY faster.
    My code looks like this (I do not use the Space function, Rather the String function)

    Code:
        
    '' "Clear" the Immediate Window
    ''
        Dim i As Long
        For i = 0 To 50
            Debug.Print
        Next
        
    '' A few declarations outside of the timing loops
    ''
        Dim myFile As String: myFile = "c:\plenderjTestFile.txt"
        Dim myLof As Long, time1 As Long, time2 As Long, time3 As Long, time4 As Long
        Dim strBuff As String, strBuff2 As String, strBuff3 As String, strBuff4 As String, strBuff5 As String, currPos As Long
        
    
    '' Create a test file to work with
    ''
        Debug.Print "1/6) Creating test file ..."
        Open myFile For Output As #1
            For i = 0 To 50000
                Print #1, i
            Next
        Close #1
        Debug.Print "2/6) Created! , File length : " & FileLen(myFile)
        
        
        myLof = FileLen(myFile)
        
    '' First test. Input entire length of file into memory using Input()
    ''
        Debug.Print "3/6) Input(Lof(), #fileNumber)"
        time1 = GetTickCount
        Open myFile For Binary As #1
            strBuff = Input(myLof, 1)
        Close #1
        time1 = GetTickCount - time1
                
            
    '' Second test. Read in and append 4096 blocks to a buffer using a Do...Loop
    ''
        Debug.Print "4/6) Now using normal Do...Loop"
        time2 = GetTickCount
        Open myFile For Binary As #1
            Do Until Loc(1) = myLof
                strBuff2 = strBuff2 & Input(4096, 1)
            Loop
        Close #1
        time2 = GetTickCount - time2
                
                
    '' Third test. Read in and append 4096 blocks to a buffer using a Do...Loop and special code for appending
    ''
        Debug.Print "5/6) Now using modified Do...Loop"
        time3 = GetTickCount
        strBuff4 = Space(myLof)
        Open myFile For Binary As #1
            Do Until Loc(1) = myLof
                strBuff3 = Input(4096, 1)
                CopyMemory ByVal StrPtr(strBuff4) + currPos, ByVal StrPtr(strBuff3), LenB(strBuff3)
                currPos = currPos + LenB(strBuff3)
            Loop
        Close #1
        time3 = GetTickCount - time3
    
    '' Fourth Test
        Debug.Print "6/6) Now using GET, NO LOOP"
        time4 = GetTickCount
        Open myFile For Binary As #1
            strBuff5 = String(myLof, vbNull)
            Get #1, , strBuff5
        Close #1
        time4 = GetTickCount - time4
        
    '' Display results, and confirm that resulting file buffers are equal
    ''
        MsgBox "Code times : " & vbCrLf & _
                "  Input(Lof(), #filenum)" & vbTab & time1 & vbCrLf & _
                "  Normal Do...Loop    " & vbTab & time2 & vbCrLf & _
                "  Modified Do...Loop  " & vbTab & time3 & vbCrLf & _
                "  And when using GET" & vbTab & time4 & vbCrLf & vbCrLf & vbCrLf & _
                "  Buffer sizes  :     " & vbTab & Len(strBuff) & "," & Len(strBuff2) & "," & Len(strBuff4) & "," & Len(strBuff5) & vbCrLf & _
                "  Buffers equal :     " & vbTab & ((strBuff2 = strBuff4) And (strBuff = strBuff2) And (strBuff = strBuff5))
        
        End
    See the difference?

  25. #25
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Originally posted by plenderj
    I beg to differ !
    Most of the CRAP is posted by you - mine are all valuable posts. Buttmunch.
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  26. #26
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    alas, another meaningfull thread has been sabotaged and regulated to Chit Chat
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  27. #27

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1
    alas, another meaningfull thread has been sabotaged and regulated to Chit Chat
    You don't know what "regulated" means do you ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  28. #28
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828


    oh PANTS!!!!

    i spelt it wrong

    relegated (probably still spelt wrong)


    PS: up yours
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  29. #29

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1


    oh PANTS!!!!

    i spelt it wrong

    relegated (probably still spelt wrong)


    PS: up yours
    bah. suck me right off !
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  30. #30
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    mad oirishman
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  31. #31

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    c'est moi
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  32. #32
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    er, what part of France is Ireland in?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  33. #33

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Kinda over on the side...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  34. #34
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    sacré bleu!!! vous suis imbecile.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  35. #35
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    vous ETES un embicile ?

    vous etes un tete de merde!

    te-hee

  36. #36

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1
    sacré bleu!!! vous suis imbecile.
    vous êtes. not vous suis.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  37. #37
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by yrwyddfa
    vous ETES un embicile ?

    vous etes un tete de merde!

    te-hee
    je nes compredes pas

    i used to say that alot in school
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  38. #38
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by yrwyddfa
    vous ETES un embicile ?

    vous etes un tete de merde!

    te-hee
    je nes comprendes pas

    i used to say that alot in school
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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