Wednesday 19 January 2011

ASP: How long does my asp code take?

   


Ever wondered how long does it take to run an ASP snippet? What a simple question! Ever needed to know? Have you ever thought that you really need to know it?
I'm not getting crazy, believe me. Those are simple questions, but rarely programmers know about a very simple way to calculate - in milliseconds! - how long it took to run a query, for example. Well, you might wonder if anyone really has ever cared about it. Are you sure? Let's try to imagine a situation where you might find it useful.


Optimizing a query
You've just finished polishing your newly created SQL query. You're proud of it and happy to have finally found a solution to that annoying issue. Great! Now you can think about how that new query is really performing in different situations. Test your query in a live environment and print out how long does it take to output the recordset.

Let the user know
Sometimes you might need to calculate the elapsed time, just to make the user aware of the time needed to fulfill the submitted request. You probably saw many times on forums: Task completed in xx seconds.

The code
In order to reach our goal, we are going to use the VBScript Timer() object. It is very simple! Before starting your task, insert these few lines:
<%
Dim start_time, end_time, elapsed_time
start_time = Timer()
%>
After that, just put your code and after your code, put:
<%
end_time = Timer()
elapsed_time = end_time-start_time
response.write "Task completed in " & elapsed_time  & " seconds"
response.write "(" & elapsed_time*1000 & " milliseconds)"
%>
And you are done. Enjoy and let me know what you think in the comment section.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.