ASPNL logo (1 kb)
Thursday, November 20, 2008

powered by


Microsoft ASP.NET Connections
Member of ASP Guild
<< previous | index | next >>
How fast is my code?

How fast is my code?

By Michiel van Otegem
20 June 2001

It's very important to know how fast your code, or part of it, runs. It makes it easier to see which operations have to change, to handle many users on your site. Even though speed measurement is no replacement for testing under (heavy) load, it could help you during the developement of an application to identify and correct bottlenecks in advance. For real robust tests you should use a stress-tool like the Web Application Stress Tool.

There are several (COM) objects to measure speed, like Profiling Component 1.1. These components are more accurate then time measurements with script. Reason for this is that time measurement on the basis of script is dependent on the internal clock of the computer, which works on the basis of an interval every 55 milliseconds. Your measurement will therefore only be accurate to 55 milliseconds.

The code below shows you how to measure with VBScript and JScript:

metervbscript.asp
<%
Option Explicit

Dim Starting point
Dim i

Starting point = Timer
For i = 1 To 1000
   Response.Write("Do something!")
Next

Response.Write("execution time: " & (Timer - Starting point) & " seconds")
%>


meterjscript.asp
<SCRIPT LANGUAGE=JScript RUNAT=Server>
function measuring point() {
//return date/time in milliseconds sinds 1970
   var d = new Date();
   return d.getTime();
}

var starting point = measuring point();

for(var i=0; i < 1000;i++) {
   Response.Write("Do something!");
}

Response.Write("execution time: " + (measuring point() - starting point) + " milliseconds")
</SCRIPT>


If you test this script you'll see the text "Do something!" a 1000 times and after that a mention of the number of milliseconds the script took to execute. If you refresh it a couple of times, you'll see the time isn't always the same. So you'll have to execute it a couple of times and take an average to get a good idea of the speed. Be aware that the script has to be loaded and compiled into the server memory at least once. The second time the script runs, this probably won't be necessary and will therefore be a bit faster.

<< previous | ^ to top | index | next >>
copyright 2000-2002 ASPNL