Many people have asked how they can time their ASP script, to see how efficient they are (even down to the millisecond). Here are two techniques to do this (one has millisecond accuracy, and one can be used to derive it indirectly).
The first method uses the timer() object, introduced in VBScript version 5.0. This returns the number of seconds that have passed since midnight -- sounds useless, but the value contains a sparsely documented gem: milliseconds! Yes, you heard right. So you can use script like the following to measure the number of milliseconds that pass between the start and end of some arbitrary block of code.
<% ' get timer before task begins: starttime = Timer() ' do some task, e.g.: Do While z < 350000 z = z + 1 Loop ' get timer after task completes: endtime = Timer() ' display results: Response.Write "The task completed in " & endtime-starttime & " s" Response.Write " (" & (endtime-starttime)*1000 & " milliseconds)." %> |
Make sure you put some substantial code in there to test that this really works (most simple tasks don't take long enough to measure a difference; it took 350000 iterative addition calls to register 1 second of process time on my machine). Change the number in the loop above and you will see the difference.
The second method measures the number of times a certain task can be executed within a given number of seconds.
<% ' set number of seconds: numSeconds = 5 ' set counter: counter = 0 ' get starttime: starttime = now() ' set endtime to some time more than numSeconds in the future: endtime = dateadd("n", numseconds + 1, starttime) ' loop until numSeconds have passed, resetting endtime each loop: Do Until DateDiff("s", starttime, endtime) = numSeconds endtime = Now() counter = counter + 1 Loop ' display results: Response.Write "In " & numSeconds & " seconds, the task completed " & counter & " times." Response.Write "<br>(Average time per loop: " Response.Write FormatNumber(numSeconds*1000/counter,0,-1,0,-1) & " milliseconds.)" %> |
[Side note A: Don't forget to weigh in datetime and other calculations within your loops, they add CPU time to the process as well. To see the difference, add the 'sometime = Now()' within the first script above, and note the performance difference. In my tests, a simple call to Now() more than doubled the time required to finish the loop.]
[Side note B: Speed of a script is a very small percentage of the battle -- you need to test your scripts inder heavy load and make sure the server can handle it. Because if the server goes down, Joe User isn't going to be waiting an extra millisecond or two to get his results; he's going to be waiting until your pager goes off at 4 a.m. and you fix the problem!]
If you require more advanced performance monitoring, see
Article #2513 for SQL Server monitoring specifically, and
Article #2139 for general ASP application performance testing.