Results 1 to 10 of 10

Thread: How to speed my code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    India
    Posts
    117

    How to speed my code

    Sir,
    i am writing a program to convert text in my richtext box to another format.

    For a = 1 To Len(frmEditor.RichText1.Text)
    If Mid(frmEditor.txtMain.Text, a, 1) = "s" And Mid(frmEditor.txtMain.Text, a + 1, 1) = "s" And Mid(frmEditor.txtMain.Text, a + 2, 1) = "{" Then
    txt = txt & "GR"
    ElseIf Mid(frmEditor.txtMain.Text, a, 1) = "s" And Mid(frmEditor.txtMain.Text, a + 1, 1) = "s" Then
    txt = txt & "LM"
    ElseIf Mid(frmEditor.txtMain.Text, a, 1) = "s" And Mid(frmEditor.txtMain.Text, a + 1, 1) = "s" Then
    txt = txt & "MT"
    ....................
    ..................
    ........................
    ...................
    Else
    txt = txt & Mid(frmEditor.txtMain.Text, a, 1)
    Endif


    above is an abstract of my code. what my problem is when converting large text it takes very much time. how can my speed up my program? i wan to check one character by character. please help....

    raman

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

    Re: How to speed my code

    Why are you extracting adjacent characters one by one? Why not compare string composed of 3 characters to "as{"?

    Your working with the entire text stream even if your referring to just a few characters in the middle, and manipulating the end. Can't you work with a string array equivalent by using Split(text, vbCrLf)? With a string array you can redim a workign array of same size and store the "GR", "LM", "MT' setc strings there. Then later do just one concatenation, Join(source_array, vbCrLf) & Join(workign_array, vbCrLf).

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    India
    Posts
    117

    Re: How to speed my code

    sir,
    will u please explain more...
    how can i rewrite the code as u said.... i don't know vb thoroughly... plz help....
    please rewrite my above code...

    ram

  4. #4
    Addicted Member
    Join Date
    Jul 2006
    Location
    Adelaide, Australia
    Posts
    204

    Re: How to speed my code

    anywhere that you have a "mid" or "left" or "right" change it to "mid$" or "left$" or "right$"

    The $ tells VB that it is a string so you speed the process up because it doesnt need to convert the variable types (or something like that).

    also I think changing a series of elseif's into a select case statement will improve speed as it wont repeat the check for every single statement.

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

    Re: How to speed my code

    You algorithm is slow because:

    - it repeatedly accesses Text property of an object instead of a variable declared as native data type string (or copy Text first to a string variable then work with that and not richtextbox.Text).

    - your doing repetitive string concatenation. Strings are immutable. Every time you concatenate a new memory location is allocated for result string and old strings are garbage collected. And as the string grows it becomes harder and longer to find a memory location to accommodate growing string.

    - the logic itself is inefficient, as mentioned earlier you could have compared all three characters in one go but had to extract them one at a time; multiple Mid() calls instead of just one. As you go farther and farther away from string start Mid() performance degrades, and you suffer the degradation x3.

    - most string functions suffer performance degradation as string grows. So try to use Split() and work with shorter strings. To rebuild string use Join().

    - there's also performance penalty in data type conversion. As mentioned by Macka007, Mid$() returns a string instead of default Variant, so no implicit conversion to string necessary.

    - since your currently always appending to the end of the text, why not build the trailing text separately (such as in another array) then concatenate them together AFTER all other processing is done and NOT FOR EACH CHARACTER processed.
    Last edited by leinad31; Oct 8th, 2007 at 10:02 PM.

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

    Re: How to speed my code

    InStr() and InStrB() are some of the faster string functions; Len() and LenB() are fastest. You might want to consider building an intermediate output based on use of InStr() such as InStr(1,source_string, "ss{"), and then create the string to be appended to richtextbox.text from the intermediate output (so they are ordered properly).

    An InStr() based algorithm reduces the number of tests/comparisons made at the cost of additional complexity and intermediate output; why bother checking if 3 characters are "ss{" as many times as length of string when "ss{" doesn't exist in string to begin with or Not InStr(1, string, "ss{") > 0.

    Just benchmark later on which is faster; per character/Mid() iteration, or iterated InStr() implementation.
    Last edited by leinad31; Oct 8th, 2007 at 04:52 AM.

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: How to speed my code

    You already got most answers, but it basically comes down to something like:

    Code:
    Dim str As String
    str = frmEditor.txtMain.Text
    For a = 1 To Len(str)
        If Mid$(str, a, 3) = "ss{" Then
            txt = txt & "GR"
        ElseIf Mid$(str, a, 2) = "ss" Then
            txt = txt & "LM"
        ElseIf Mid$(str, a, 2) = "ss" Then
            txt = txt & "MT"
    ....................
    ..................
    ........................
    ...................
    
        Else
            txt = txt & Mid(str, a, 1)
        End If
    Next
    I just assumed that frmEditor.RichText1.Text should be frmEditor.txtMain.Text
    If this is not the case, the code should be a little different.

    I suspect the complete code can be replaced by a few calls to the Replace function, but without seeing the complete code, this is difficult to say.


    I noticed that the conditions in the two ElseIf statements are the same. The second ElseIf will never be true, so the code txt = txt & "MT" is never executed
    Frans

  8. #8
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How to speed my code

    As Frans C mentioned, you can use the Replace function also. It looks faster than a for/if/elseif/next loop.

    txt = Replace$(frmEditor.txtMain.Text, "ss{", "GR")
    txt = Replace$(txt, "ss", "LM")

    etc..

    but be careful with the right order of replacements, because if you replace all of the string occurence of "ss" before the occurence of "ss{" then youll get a lots of LM{ but no GR's.
    Last edited by Jim Davis; Oct 8th, 2007 at 10:00 PM.

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: How to speed my code

    yes. i agree with all of you. That being said, the biggest bottleneck is the fact it is continually accessing the object's text property. The other speed gains are negligible compared to that one.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10
    New Member
    Join Date
    Sep 2007
    Posts
    4

    Re: How to speed my code

    as i can see u are concatenating too much, try to use this class

    http://www.vbaccelerator.com/home/Vb...er/article.asp

    string builder, it is really faster

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