Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  High Speed Code, Scalable Code...
     [next Lesson]  Speed: Coding Tips

Speed: Measuring Code Speed
by Richard A. Lowe drahcir@home.com
  with help from Jonathan McGuire jmcguire@solutionsatimpact.com
  Gregory Lybanon glybanon@sbcsystems.com
  Charles Carroll charlescarroll@learnasp.com

Measuring speed to the millisecond was considered impossible with ASP. People built COM components that wrapped up API calls! So we called a COM component that called an API and then we distorted the measurement with overhead. Scripting has a few tricks up its sleeves yet as Richard demonstrates with the nifty Library implemented in Jscript. We will use it to time retrieving and displaying identical data three different ways:

  • traditional loop and movenext
  • one getrows call
  • one getstring call

This method is great for testing optimizations to 1 script but does not show how the script will run when many users are simultaneously executing it. Tools like the Stress Tester at:
http://homer.rte.microsoft.com
are great for simulating and recording measurements for multi-user performance.

Here we retrieve data using a traditional loop (/freebook/asp/dbtable.aspx):

   filename=/learn/test/timedbtable.asp

<Test Script Below>


<html><head>
<TITLE>timedbtable.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<!--#include file="lib_timethis.asp"-->
<%
Set HttpObj = Server.CreateObject("AspHTTP.Conn")
HttpObj.Url = "http://www.learnasp.com/learn/test/dbtable.asp"
timeThen = milliDif()
strResult = HttpObj.GetURL
timeNow = milliDif()
SET HTTPobj = nothing
elapsed=timeNow-timeThen
msg="<br>Process time in ms: " & elapsed & "<br>" & elapsedpretty(elapsed)
bodytag="<body bgcolor=""#FFFFFF"">"
STRresult=replace(STRResult,bodytag,bodytag & msg) 

response.write STRresult

%>
</body></html>

Here we retrieve data by fetching all the data into an array in one "gulp" (/freebook/asp/dbtablegetrows.aspx):

   filename=/learn/test/timedbtablegetrows.asp

<Test Script Below>


<html><head>
<TITLE>timedbtablegetrows.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<!--#include file="lib_timethis.asp"-->
<%
Set HttpObj = Server.CreateObject("AspHTTP.Conn")
HttpObj.Url = "http://www.learnasp.com/learn/test/dbtablegetrows.asp"
timeThen = milliDif()
strResult = HttpObj.GetURL
timeNow = milliDif()
SET HTTPobj = nothing
elapsed=timeNow-timeThen
msg="<br>Process time in ms: " & elapsed & "<br>" & elapsedpretty(elapsed)
bodytag="<body bgcolor=""#FFFFFF"">"
STRresult=replace(STRResult,bodytag,bodytag & msg) 

response.write STRresult

%>
</body></html>

Here we retrieve data by asking the backend to combine the data into a custom string and not even bring fields and rows, just produce 1 string (/freebook/asp/dbtablegetstring.aspx):

   filename=/learn/test/timedbtablegetstring.asp

<Test Script Below>


<html><head>
<TITLE>timedbtablegetstring.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<!--#include file="lib_timethis.asp"-->
<%
Set HttpObj = Server.CreateObject("AspHTTP.Conn")
HttpObj.Url = "http://www.learnasp.com/learn/test/dbtablegetstring.asp"
timeThen = milliDif()
strResult = HttpObj.GetURL
timeNow = milliDif()
SET HTTPobj = nothing
elapsed=timeNow-timeThen
msg="<br>Process time in ms: " & elapsed & "<br>" & elapsedpretty(elapsed)
bodytag="<body bgcolor=""#FFFFFF"">"
STRresult=replace(STRResult,bodytag,bodytag & msg) 

response.write STRresult

%>
</body></html>

The library that accomplishes this:

   filename=/learn/test/lib_timethis.asp

<Test Script Below>


<SCRIPT LANGUAGE=JScript RUNAT=Server>
function y2k(number)    {
    return (number < 1000) ? number + 1900 : number;
                            }
function milliDif()    {
    var d = new Date();
   return d.getTime()
                        }
                        
function elapsedpretty(parm1)
{
 var elapsedsecs = 0
 var elapsedmins = 0
 
 elapsedsecs=Math.floor(parm1/1000)
 parm1=parm1%1000
 
 elapsedmins=Math.floor(elapsedsecs/60)
 elapsedsecs=elapsedsecs%60
 
 
elapsedpretty=elapsedmins + " minute"
if(elapsedmins!=1)
    elapsedpretty=elapsedpretty+"s"
 
elapsedpretty = elapsedpretty+" " + elapsedsecs+" second"
if(elapsedsecs!=1)
    elapsedpretty=elapsedpretty+"s"
 
elapsedpretty = elapsedpretty+ " "+parm1+" millisecond"
if(parm1!=1)
    elapsedpretty=elapsedpretty+"s"
 
 return elapsedpretty;
} 
</script>

Send Us a Holiday Gift!. Charles celebrates Christmas, Jewish holidays, Kwanza, Chinese New Year, Japanese Holidays, Secretary Day, High Muslim Holy Days, Pagan & Wicca holidays, and many more! - send a gift any HOLIDAY. 2004 gift gallery & 2005 gift gallery