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 queryYou'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 knowSometimes 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 codeIn 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:
After that, just put your code and after your code, put:
Dim start_time, end_time, elapsed_time
start_time = Timer()
And you are done. Enjoy and let me know what you think in the comment section.
end_time = Timer()
elapsed_time = end_time-start_time
response.write "Task completed in " & elapsed_time & " seconds"
response.write "(" & elapsed_time*1000 & " milliseconds)"