Results 1 to 7 of 7

Thread: Optimise this code Instr

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    736

    Optimise this code Instr

    Hi guys i have the following code it works like this it parses a log file for certain info and returns the required data. the log has a set of info on every person logged but this may not be in order. this info i.e Height, Age, Hair colour, Sex. every line in the log has 2 references included in it so that all data relating to a person is not confused with another persons. eg

    #1 a john is a man
    #1 b Jackie is a woman
    #1 c Peter is a man
    #2 a Height of john is 5Ft
    #2 b Height ofJackie is 5ft 3"
    #3 c Jackie is Blond
    #7 j peter is 39
    #1 a John is 27

    one small thing need to be concidered the refernce points in the log i.e #1,2,3
    and a,b,c ect are formated different in some lines to others so i have had to split the reference points at spaces to #1, #2, #3 a,b,c these in the code are txtRefA and txtRefB

    so now to the code it looks for Height, sex, haircolour ect based on the 2 reference points

    so with a click i get all the details on one person.
    i can change the reference points to bring details on any person i want in the log.

    VB Code:
    1. Open App.Path & "\00000712.log" For Input As #1
    2.         Do Until EOF(1)
    3.             Line Input #1, ln
    4.               If InStr(1, ln, " Name") > 0 And InStr(1, ln, txtRefA) > 0 And InStr(1, ln, txtRefB) > 0 Then
    5.                     pname = Mid(ln, InStr(1, ln, ": ") + 4)
    6.                     txtPName = pname
    7.                     uName = ln
    8.                  End If
    9.                 If InStr(1, ln, "Hair") > 0 And InStr(1, ln, txtRefA) > 0 And InStr(1, ln, txtRefB) > 0 Then
    10.                     Hcolour = Mid(ln, InStr(1, ln, "Hair"))
    11.                     txtHcolour = Mid(Hcolour, 23)
    12.                     Hcolour = ln
    13.                 End If
    14.                 If InStr(1, ln, " ShoeSize") > 0 And InStr(1, ln, txtRefA) > 0 And InStr(1, ln, txtRefB) Then
    15.                     Size = Mid(ln, InStr(1, ln, "ShoeSize"))
    16.                     txtSize = Mid(Size, 15)
    17.                     Size = ln
    18.                 End If
    19.                 If InStr(1, ln, "Sex") > 0 And InStr(1, ln, txtRefA) > 0 And InStr(1, ln, txtRefB) > 0 Then
    20.                     Sex = Mid(ln, InStr(1, ln, "Sex"))
    21.                     Height = Split(Replace(Sex, ":", " "), " ")
    22.                 End If

    the code works fine takes about 8 seconds to pull all info from 20mb log with ref (A) & (B). but to get next person info i change reference points and click again. to basically i am reloading the file and searching it. if i am not mistaken if i was to be able to just search the allready loaded file every time after opening it up once this would be faster but hey you tell me i really am no expert so if poss please keep any suggested changes to this code to a minimum.
    my suggestion to improve it would be to open the file into the memory with one click and then just search it after changeing refA & refB for next lot of data. not sure if this is right or if so how to mod the code.

    thanks in advnce

  2. #2
    Member
    Join Date
    Dec 2005
    Posts
    63

    Re: Optimise this code Instr

    You're using Line Input, which reads one line at a time, which is why you need to reload the file for each new search. Instead of Line Input, declare a string variable (for example, called strText) and do the following:
    VB Code:
    1. strText= Input(LOF(1), #1)

    Then use InStr to parse out what you need from the strText variable.



  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    736

    Re: Optimise this code Instr

    Thanks mark i tried the following and debug show nothing

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strText As String
    3. Open App.Path & "\00000712.log" For Input As #1
    4.             strText = Input(LOF(1), #1)
    5.                 Debug.Print strText
    6.                 Close #1
    7. End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    736

    Re: Optimise this code Instr

    Any takers to this problem?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Optimise this code Instr

    looks ok, post the log file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    736

    Re: Optimise this code Instr

    i cant post the log but i can assure you its nothing to do with the log. maybe the info is loaded but its the way i write debug.print or some thing?
    anyway that is not the reason for this post please anybody got any ideas on how to make this code faster?

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Optimise this code Instr

    1) Each If statement has the exact same code (InStr(1, ln, txtRefA) > 0 And InStr(1, ln, txtRefB) > 0). Execute those statements once.

    2) If I understand correctly, there will never be more than one type ("Name", "Hair","Shoesize") on a line. of that is correct use an If...ElseIf statement block instead.

    VB Code:
    1. Open App.Path & "\00000712.log" For Input As #1
    2.         Do Until EOF(1)
    3.             Line Input #1, ln
    4.             If InStr(1, ln, txtRefA) > 0 Then
    5.                If InStr(1, ln, txtRefB) > 0 Then
    6.                    ' line contains both txtRefA and txtRefB so get the data
    7.  
    8.                    If InStr(1, ln, " Name") > 0 Then
    9.                        pname = Mid(ln, InStr(1, ln, ": ") + 4)
    10.                        txtPName = pname
    11.                        uName = ln
    12.                    ElseIf InStr(1, ln, "Hair") > 0 Then
    13.                        Hcolour = Mid(ln, InStr(1, ln, "Hair"))
    14.                        txtHcolour = Mid(Hcolour, 23)
    15.                        Hcolour = ln
    16.                    ElseIf InStr(1, ln, " ShoeSize") > 0 Then
    17.                        Size = Mid(ln, InStr(1, ln, "ShoeSize"))
    18.                        txtSize = Mid(Size, 15)
    19.                        Size = ln
    20.                    ElseIf InStr(1, ln, "Sex") > 0 Then
    21.                        Sex = Mid(ln, InStr(1, ln, "Sex"))
    22.                        Height = Split(Replace(Sex, ":", " "), " ")
    23.                    End If
    24.  
    25.                End If
    26.             End If

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