...

View Full Version : Need to optimise this code



tcadieux
12-08-2006, 02:15 AM
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

Daniel Israel
12-12-2006, 01:17 AM
Why don't you just generate the output on the fly instead of pushing strings together, redimming arrays, and then cracking them apart ??

tcadieux
12-12-2006, 01:28 AM
What would you suggest?

s_m_b
12-12-2006, 03:24 PM
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!

Daniel Israel
12-12-2006, 07:04 PM
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)

Spudhead
12-13-2006, 02:17 PM
1. Use a string concatenation class (http://www.google.co.uk/search?hl=en&q=ASP+string+concatenation+class&btnG=Google+Search&meta=)
2. Don't loop through recordsets. Use getRows() (http://www.devguru.com/technologies/ado/8678.asp) and loop through the resulting array.
3. If at all possible, use a database server rather than Access.

tcadieux
12-20-2006, 01:32 PM
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.

tcadieux
01-03-2007, 03:50 PM
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!! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum