Results 1 to 27 of 27

Thread: [RESOLVED] I need optimization/speed

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] I need optimization/speed

    This is my code, which needs to be optimized

    My function geral.ReadFile:
    Code:
    Public Function ReadFile(FileName As String) As String
    Dim Container As String, Fich As Integer
    
    Fich = FreeFile
    
    Open FileName For Input As #Fich
    Container = Input(LOF(Fich), Fich)
    Close #Fich
    
    ReadFile = Container
    
    End Function
    In my sub:
    Code:
        Original = geral.ReadFile(OriginalFile)     
        ContainerTemp = geral.ReadFile(TempFile)              
        
        Original = Right(Original, Len(Original) - 25)  
        ContainerTemp = Right(ContainerTemp, Len(ContainerTemp) - 25)              
        
        
        If InStr(1, Original, ContainerTemp, vbBinaryCompare) >= 1 Then      
            Kill TempFile                                                           
        Else                                                                        
            Kill OriginalFile                                                       
            Name TempFile As OriginalFile                               
            ListNewRegs = ListNewRegs & OriginalFile & vbNewLine                
        
        End If
    Thank you in advance for any given tip.
    Last edited by RS_Arm; Dec 6th, 2007 at 07:33 AM.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: I need optimization

    Quote Originally Posted by RS_Arm
    This is my code, which needs to be optimized
    "Optimized"??

    That implies that your code works perfectly, but it too slow, or uses too many instructions or storage space, etc.

    What do you want to achieve? Speed it up? I don't see any reason it would run slowly.

    Mac

  3. #3

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization

    What i need is speed

    If you are comparing about 60000 files, you will need it.

    Thank you
    Last edited by RS_Arm; Dec 6th, 2007 at 07:26 AM.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need optimization/speed

    Depends largely on the contents of files... try to find other means of determining deletion without having to load most of the file, or other means aside from InStr().

  5. #5

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    Thank you for your post.

    I really need to compare entire file except the 1st 25 characters in the files.
    So, do you think that I only can speed up my app using another way to determine the files that i need to delete?

    Thank you

  6. #6
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: I need optimization/speed

    It appears that your program needs to input just the last 25 characters of a file. Your method is to read the entire file into a string and take the last 25 characters from that. Given a very large file, that would be problematic, so I guess that is your optimization problem.

    Why not OPEN in BINARY, rather than INPUT?

    Then you could input the last 25 characters immediately.

    Mac

  7. #7

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    It's not really that, but you are close.
    I need to read from file system, lets say, file.txt.
    This file contains exported data from a db.
    What I do, is to recreate the content of that file in a temp file, based again in my db.

    The 1st 25 characters are the date of creation (on boths files).
    Then, I compare the files to check if there is any change.

    Thank you

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need optimization/speed

    Quote Originally Posted by RS_Arm
    Thank you for your post.

    I really need to compare entire file except the 1st 25 characters in the files.
    So, do you think that I only can speed up my app using another way to determine the files that i need to delete?

    Thank you
    Working with large strings is unavoidably slow due to memory allocation and other factors. You need other criteria... maybe the dates can be used as basis, such as skipping InStr(1, new_file, old_file) since InStr(1, old_file, new_file) is more likely to be performed.

  9. #9

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    My files have, in average, 1054 bytes.
    The date on file is only for tracking the last export, it does not refer to any field from db, so I can't take it as an valid criteria.

    Is there any faster methods, I don't know, maybe using Windows API or something like that, or is my code just in its maximum performance?

    Thank you

  10. #10
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: I need optimization/speed

    So if I get it right now, you are faced with two files, and you want to know whether they are identical, ignoring the first 25 characters.

    This undebugged function might be more useful

    Code:
    Private Function Same(f1 As String, f2 As String) As Boolean
    If Dir(f1) = "" Then Stop ' error in calling program
    If Dir(f2) = "" Then Stop ' error in calling program
    Dim ff1 As Integer: ff1 = FreeFile
    Dim ff2 As Integer: ff2 = FreeFile
    Open ff1 For Binary As #ff1
    Open ff2 For Binary As #ff2
    Dim L1 As Long: L1 = LOF(ff1)
    Dim L2 As Long: L2 = LOF(ff2)
    If L1 < 26 Or L1 <> L2 Then
      Close #ff1: Close #ff2
      Exit Function
    End If
    Dim p As Long: p = 26
    Const BufSize = 100
    Dim Content1 As String * BufSize
    Dim Content2 As String * BufSize
    Do
      Get #ff1, p, Content1
      Get #ff2, p, Content2
      If Content1 <> Content2 Then
        Close #ff1: Close #ff2
        Exit Function
      End If
      p = p + buffsize
    Loop While p < L1
    Close #ff1: Close #ff2
    Same = True
    End Function

  11. #11

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    Got error 55 in line 7
    Code:
    Open ff2 For Binary As #ff2
    Looks like vb6 doesn't like to open a 2nd file without closing the 1st.

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I need optimization/speed

    The code posted has a Typo, I think, f1 is the string with the name of the file and ff1 is the filenumber to be used!
    It should read:
    Code:
    Open f1 For Binary As #ff1
    Open f2 For Binary As #ff2
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    same error even with your correction

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I need optimization/speed

    VB6 can open multiple files ( or better Windows can do that), so what is the error given, at which line, what are the values for f1,f2,ff1,ff2?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    Run-time error '55':
    File already open
    Line 7

    f1=c:\log\teste.txt
    f2=c:\log\teste2.txt

    ff1=1
    ff2=2

    I've rebooted my computer and without doing nothing at all, still got error 55

  16. #16
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I need optimization/speed

    Are you sure that windows doesn't hold an opened file of your examples somewhere? If you not sure try to restart the PC and run the code again.

    [edit]
    just read the last lien of your post, so restart didn't help.
    Then look through your other code, did you open one of the files before the code-lines are occuring?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  17. #17

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    ok, restarting again...
    2 minutes and i'm back

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

    Re: I need optimization/speed

    Freefile should be called again only after opening the first file.

  19. #19
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I need optimization/speed

    Good point merri, I was looking in the wrong direction, that should solve his problem!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  20. #20

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    merri, you got it.
    Now is running smoothly.

  21. #21

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    Now I'm going to addapt it to my app. In some minutes I'll give you all more feedback about speed processing.

    Thank you guys for your interest.

    brb

  22. #22
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: I need optimization/speed

    Your files are rather small, so I believe reading them all in beyond the offset position in binary should be very fast. Consider modifying this alternative:
    Code:
    Dim FF1 As Integer, FF2 As Integer
    Dim Data1 As String, Data2 As String
    Const OffSet = 25
    
    Private Sub Command1_Click()
    Call CompareFiles
    End Sub
    
    Public Sub CompareFiles()
    FF1 = FreeFile
    Open "MyFirstFile" For Binary As #FF1
    FF2 = FreeFile
    Data1 = Space$(LOF(FF1) - OffSet)
    Get #FF1, OffSet + 1, Data1
    Open "MySecondFile" For Binary As #FF2
    Data2 = Space$(LOF(FF2) - OffSet)
    Get #FF2, OffSet + 1, Data1
    Close
    If Data1 <> Data2 Then
        MsgBox "Files are not the same after offset."
    Else: MsgBox "Files match after offset."
    End If
    End Sub
    Doctor Ed

  23. #23

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: I need optimization/speed

    I'm pretty satisfied with the suggestion from Mr. Mac. It only takes 7 minutes to read 31001 files and compare to other 31001.

    Although I'm gonna test your suggestion CodeDoc.

    Thank you all guys.
    I'm in debt to you all.

  24. #24
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: [RESOLVED] I need optimization/speed

    Thanks for your reply, RS_Arm.

    Sorry to disconvenience you by posting untested code. I had a doctor appointment and it is snow/ice on the road, so I had to leave immediately.

    I hate people that post untested code. Now I see why.

    Glad everyone could patch it up.

    Mac

  25. #25
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: I need optimization/speed

    Quote Originally Posted by RS_Arm
    I'm pretty satisfied with the suggestion from Mr. Mac. It only takes 7 minutes to read 31001 files and compare to other 31001.

    Although I'm gonna test your suggestion CodeDoc.

    Thank you all guys.
    I'm in debt to you all.
    Take a look at this while you are at it. CV Michael's code may run faster yet:
    http://www.vbforums.com/showthread.php?t=498745

    I tested it on a huge file and was rather amazed at the speed. Byte array comparison will probably outrun string comparison.
    Doctor Ed

  26. #26
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] I need optimization/speed

    Hi Mr.Mac
    If only tested code would be allowed to be posted, none of those who are online but without their IDE on hand could be posting. And even tested code could have an error, we are all humans, we do make mistakes, but by helping each other we all will improve. (Posted in english without my english grammar- and wordbook on hand)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  27. #27

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: [RESOLVED] I need optimization/speed

    Thank you Code Doc. Indeed, CV Michael's code use much less memory then the other codes (-120mb RAM).
    About its speed, it takes the same time (7 mins for 31001 files).

    Thank you once again.

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