When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Monday, December 27, 1999

Timing the Execution Times of your ASP Scripts

By Mike Shaffer


Since my last article, several people have asked how to perform benchmark timing within the ASP (VBScript) environment. This article will examine some of the options available for stopwatch-like timing routines. It will also present an easy to incorporate solution that uses no components and allows up to 20 individual stopwatches to be running simultaneously.

- continued -

'

Actually, there are several possible solutions for the task of timing VBScript routines and functions. You can obtain ActiveX components that act as a stopwatch, but that solution will usually cost you a few dollars. It would also force you to use a component that has to be registered on the server (which many people cannot do because of hosting restrictions) and/or would force you to use a component for which you have no source code.

You can also use Javascript within the HTML page to perform timing. This has the drawbacks of being less portable (because you are relying on the browser's implementation of Javascript) and somewhat less accurate (too many dependencies on network speed, browser/machine/interpreter efficiency, etc.)

One gentleman suggested grabbing the current time (TIMEVALUE(NOW)) at the beginning and the end of a section of code and using the DATEDIFF function to obtain elapsed times. Unfortunately, this approach can only be accurate to approximately 1/2 second in any given case (when averaging times over several iterations), so is probably not suitable.

Fortunately, there is a very simple way to perform timing on your ASP code.

Examine this code:

Dim Iterations, StartTime, ElapsedTime, AverageTimePerIteration, X

Iterations = 1000
StartTime = Timer

For X = 1 TO Iterations
   '... do operations to be timed
Next

ElapsedTime = Timer - StartTime
AverageTimePerIteration = ElapsedTime / Iterations

The TIMER function returns the elapsed time in seconds (and fractional seconds) since Midnight on the previous day. An important fact to keep in mind with the TIMER function is that it is only accurate to roughly 50 milliseconds. As a result, if you want to time a routine or a certain patch of code with any accuracy that is useful, you will want to do multiple iterations of that code (similar to the example above) and then divide the elapsed time by the number of iterations (this yields an average time per iteration that is reasonably accurate).

When using the TIMER function to benchmark certain sections of code to determine which is more efficient, we are usually looking for relatively LARGE differences in time. For example, I don't really care if, over 10,000 iterations of a routine, one method outperforms the other by .002 seconds overall. So in the final analysis, for this type of benchmarking, the actual accuracy of the timer is not nearly as important as its timing consistency.

Here, then, is some code that will help you get started in timing your own routines. To use this code, simply place this line in your variable declaration section:

Dim StopWatch(19)

and then include these two routines:

sub StartTimer(x)
'::::::::::::::::::::::::::::::::::::::::::::::::::::
':::     Here we start the timer                  :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::
   StopWatch(x) = timer
end sub


function StopTimer(x)
'::::::::::::::::::::::::::::::::::::::::::::::::::::
':::     Here we 'stop' the timer and calculate   :::
':::     the elapsed time (allowing for the       :::
':::     midnight wrap-around. NOTE: These        :::
':::     routines should not be used to time      :::
':::     very long events (>1 hour) or very       :::
':::     short events (< 100 milliseconds)        :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::
   EndTime = Timer

   'Watch for the midnight wraparound...
   if EndTime < StopWatch(x) then
     EndTime = EndTime + (86400)
   end if

   StopTimer = EndTime - StopWatch(x)
end function

(Note: You could keep this code snippet as an INCLUDE file.)

With this code, you have access to 20 individual stopwatches (you can easily increase that number to whatever you wish by changing the DIM statement). To start a stopwatch, use:

StartTimer x

where x is the number of the stopwatch you wish to use (example: to start stopwatch #7, use StartTimer 7).

When you're ready to end the stopwatch and get the elapsed time, simply reference the StopTimer function, e.g.:

Elapsed = StopTimer(x)
Response.Write "Elapsed time was: " & Elapsed

Notice that the StopTimer function does not reset the initial time. Calling the StopTimer multiple times (without resetting the timer with StartTimer) will continue to return total elapsed time for that particular timer. If you like, you could add this line to the end of the StopTimer function to make the timer automatically reset (and act more like a 'lap' counter):

StopWatch(x) = Timer

Summary: This is a very easy to use timing system that is free and uses no components. It is reasonably accurate, especially when averaged over multiple iterations of a function. It is also very easy to use, and did I mention, it's free?

Remember... Optimize 'til you die!

Happy Programming!


Attachments:

  • Download the source code for the Timing Script in Text Format



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article