Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need to optimise this code

    The below code works, but becuase i have about 800 items in the Glossary, the page takes about 5-7 seconds to load......is there a way I could optimise the below code?









    '<---------- DO NOT EDIT THIS INFORMATION

    IF Not objPagingRS.EOF THEN

    Article=objPagingRS("Article")

    '****************************************************************
    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
    conn.Open ("Glossary.mdb")


    sQUERY="SELECT Glossary.GlossaryID, Glossary.Term, Glossary.Desc FROM Glossary;"

    set rsDefinitions=Server.CreateObject("ADODB.recordset")
    rsDefinitions.Open sQUERY, conn

    defArr = Array()
    While Not rsDefinitions.EOF
    If Not IsNull(rsDefinitions("Term")) Or rsDefinitions("Term") <> "" Then
    If in_array(rsDefinitions("Term"),defArr) = False Then
    Redim Preserve defArr(i)
    defArr(i) = rsDefinitions("Term") & "," & rsDefinitions("GlossaryID")
    i=i+1
    'Response.Write rsDefinitions("Glossary_Term_E") & "<br>"
    End If
    End If
    rsDefinitions.MoveNext
    Wend

    rsDefinitions.close
    conn.close
    '****************************************************************

    Response.write(CreateLink(Article,defArr))




    Function CreateLink(str, arr)
    temp = str
    For i=0 To Ubound(arr)

    If InStr(arr(i),",") Then
    iWhere=Instr(arr(i),",")
    URL=Mid(arr(i),iWhere+1)
    newTerm=LEFT(arr(i),iWhere-1)

    END IF

    If InStr(LCASE(str),LCASE(newTerm)) Then
    'temp = Replace(temp,newTerm, "<a href='"&Url &"'>" & lCASE(newTerm) & "</a>",1, -1, vbTextCompare)
    temp = Highlight(temp, newTerm, "<a href='glossary.asp?id="&Url &"' Class='Underline' >", "</a>")

    End If



    Next
    CreateLink = temp
    End Function

    Function in_array(element, arr)
    For i=0 To Ubound(arr)
    If Trim(arr(i)) = Trim(element) Then
    in_array = True
    Exit Function
    Else
    in_array = False
    End If
    Next
    End Function


    Function Highlight(temp, newTerm, strBefore, strAfter)

    Set re = New RegExp
    re.Pattern="\b("&newTerm&")\b"
    re.IgnoreCase=True
    re.Global=True
    strOutput=re.Replace(temp,strBefore&"$1"&strAfter)
    'Response.Write("<pre>" & strOutput & "</pre>")
    Highlight=strOutput
    End Function
    Last edited by tcadieux; 12-08-2006 at 08:08 PM. Reason: things have chnaged

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Why don't you just generate the output on the fly instead of pushing strings together, redimming arrays, and then cracking them apart ??

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What would you suggest?

  • #4
    New to the CF scene
    Join Date
    Jul 2002
    Location
    York, England/UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    concatting strings is bad, so rather than using the createlink function for the array, switch it to just do its job on the fly instead. No need to mess with building the array and then readingt through it for the sake!

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Quote Originally Posted by tcadieux View Post
    What would you suggest?
    Which part is not clear? I thought what I said was pretty straightforward.

    (Are you an ASP novice? You didn't mention that, so I presumed that general advice would be sufficient)

  • #6
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    1. Use a string concatenation class
    2. Don't loop through recordsets. Use getRows() and loop through the resulting array.
    3. If at all possible, use a database server rather than Access.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm very sorry, i've been off sick and i think i was a little loopy when i wrote this. As you can see from how i wrote this, i'm still learning, therefore, yes, if someone could be a bit more specific, i would appreciate it, thank you.

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    I just wanted to mention that i broke out the code as suggested and used only GetRows and the page loads as fast as any other on the site now.

    Thankx!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •