6

Today I was tasked to improve the performance of a classic ASP page. Rewriting the code in ASP.NET is at this moment not an option so I took up the challenge to squeeze every ounce of performance I can get out of the page.

The page consists of the basic "SELECT bla bla FROM bla" into a couple of recordssets. A while loop iterates through those recordsets and dumps <tr><td> strings. Within the while loop there is a bunch of conditionals and whatnot. There are 3 subroutines that are called which use global variables (not local variables passed as parameters).

So nothing really shocking or anything. Before I started my optimization the loop took around 15 seconds to complete. Of the 15 seconds around 6 were taken up by the sql query.

After changing a few things around I managed to get it around to 7 seconds.

The things that I have changed around are:

  • Instead of doing SELECT *, I selected only the columns that I needed. The query went down to on average 4 seconds. It's a pretty heavy query with views within views.

  • I removed all the context switching within the loop. So I changed things like <%= bla %> to Response.Write(bla).

  • The 3 subroutines were defined as functions, but they were used as sub (with no result). So I changed the functions to subs. Does that help?

After making my changes I found that most of the time was taken up by one of the subroutines. I didn't have time enough today to change the subroutine, but it consists of things like:

  • Date functions: Dateadd, Datediff
  • Array functions: Ubound(arr) and index referencing: arr(I)
  • String functions: left, mid, right, lower, replace

With every page call, that subroutine is run around 1600 times.

Anybody out there have any experience with optimizing classic asp pages? Do you have any good tips for optimization? What I'm looking for is improvement of code within a do ... loop statement.

I'm an experienced ASP.NET developer and know quite a bit about perfomance improvement in ASP.NET. Classic ASP uses a different "engine" so I was wondering if anybody out there have any insights into improving the performance of classic ASP.

Thanks!

M

PS: Yes I know that classic ASP uses VBScript

6 Answers 6

9

GetRows This will create the speed you seek. Here are some other tips I have used.

5

Seeing that this is a popular question I've decided to explain what I did 3 years ago that sped up the ASP script.

The original script made heavy use of resizable arrays in order to store key-values, so I modified that code to use Scriting.Dictionary. Example:

Dim myDictionary
Set myDictionary = Createobject("Scripting.Dictionary") 
myDictionary.item("key") = "value"

That is a lot faster then resizable arrays.

Another big change is the concatenation of strings. The original script was full of:

S = ""
S = S & "First line<br />"
S = S & "Second line<br />"
S = S & "Third line line<br />"
Response.Write(S)

I modified this to:

Response.Write("First line<br />")
Response.Write("Second line<br />")
Response.Write("Third line<br />")

This made a huge difference. The concatenation of strings is a huge bottleneck because the string is thrown away and then reinitialized.

Another option is:

S = "First line<br />" & _
        "Second line<br />" & _
        "Third line line<br />" 
Response.Write(S)

This is also a good tip for ASP.NET: Use a StringBuilder instead of concatenating strings.

Another important change is context switching. The original code was full of:

<table>
    <tr>
        <td><%= rs("Col1") %></td>
        <td><%= rs("Col2") %></td>
        <td><%= rs("Col2") %></td>
    </tr>
</table>

The 3 context switches take up a lot of time so I modified to this:

<%
Response.Write("<table>")
Response.Write("<tr>")
Response.Write("<td>")
Response.Write(rs("Col1"))
Response.Write("</td>")
Response.Write("</tr>")
Response.Write("<tr>")
Response.Write("<td>")
Response.Write(rs("Col2"))
Response.Write("</td>")
Response.Write("</tr>")
Response.Write("<tr>")
Response.Write("<td>")
Response.Write(rs("Col3"))
Response.Write("</td>")
Response.Write("</tr>")
Response.Write("</table>")
%>

Yes the code is very redundant but it performs better.

Another small modification (which is actually a dirty hack) is to use WITH (NOLOCK) in your SQL queries:

conn.Query("SELECT * FROM MyTable WITH (NOLOCK) LEFT JOIN AnotherTable WITH (NOLOCK) ON MyTable.Id = AnotherTable.Id")

It makes a difference.

Finally, I don't know if that helps much, but there was a lot of copy-pasted code that I refactored into clean functions.

I hope that people finding this thead will find these tips useful.

1
  • My comment got downvoted to 0? Really? Did the person who downvoted actually read the thread?
    – mghaoui
    Feb 5, 2013 at 20:49
4

With every page call, that subroutine is run around 1600 times.

I'd say that's pretty much the whole problem, but without knowing the details of the data returned the query, what that subroutine does, and why it needs to be done 1600 times for a page, it's hard to suggest much to lower it.

1
  • It's for a planning application and the person who built it enthousiastically created a big while loop that shows the planning of a lot of records for an entire month (30 columns and around 20 rows).
    – mghaoui
    Feb 26, 2009 at 19:23
3

I'm marking MrChrister's answer as the answer to my question "Do you have any good tips for optimization?". The tips there are good and it managed to speed up the script by 2 seconds.

I found out eventually what was making the script slow. In the while loop the programmer was doing Filter(Array) a lot. He was basically using Filter(Array) to look up key/value pairs.

So the final solution was changing the code of Filter(Array) to using the "Scripting.Dictionary" object. It sped the code up by a factor of 12.

Thanks for all your replies.

M

0

If you really think the problem is in that 1 function, why don't you put the code here so people can make optimization suggestions.

3
  • Unfortunately I don't own the code. I'll probably get in trouble for posting it. The function basically builds up strings based on the recordset but splicing and conctenating and then dumping it with response.write. Standard stuff.
    – mghaoui
    Feb 26, 2009 at 19:27
  • Unfortunately also, it's hard to give suggestions when you're doing standard stuff and we have no access to the code. :) Just like Chad said, maybe you can think more about the "why". Or maybe you can try moving a bunch of the operations to a stored procedure. Feb 26, 2009 at 23:02
  • I wonder if it's the concatenating that's causing the problem. VB is known for not handling concat' of strings well... Look into an alternative method of writing to the screen or creating long strings (think array or stream object).
    – Cirieno
    Feb 27, 2009 at 17:30
-3

I experienced that in most cases you can gain performance when using a StringBuilder within classic ASP. There is a nice StringBuilder implementation for classic ASP within the ajaxed library. It uses the .net StringBuilder. Thats pretty cool and easy to use:

<%
set output = new StringBuilder
do
  output("some output")
loop
response.write(output.toString())
%>

I would recommend using ajaxed library if you need to do some tweaking. It's setup quickly and offers you a lot tools for classic ASP. E.g. Maybe you could also gain some performance when using AJAX (or at least the impression of performance).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.