Timing the Execution Times of your ASP Scripts
By Mike ShafferSince 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.
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:
|
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:
|
(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) |
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: