views:

424

answers:

6

What is a good way to measure code execution time in VBScript?

Or failing that how to do it in JavaScript?

+1  A: 

For JavaScript, use the Firebug or IE profilers, or free DynaTrace AJAX edition profiler.

Craig Stuntz
A: 

This really depends on what you are trying to measure?

If are testing something and want to know how long certain pieces go, you could just declare a start and stop time variable, then do a difference between them....

Mitchel Sellers
A: 
d=new Date(); x=0; for (i=0; i < 1e7; ++i) { x += i; } d2=new Date(); d2-d

12296

Use the Date object, which returns a valueOf() in milliseconds since Jan 1 1970. You can then subtract times to get elapsed time in milliseconds.

Jason S
Note: Not 100% accurate http://ejohn.org/blog/accuracy-of-javascript-time/
Josh Stodola
+4  A: 

For JavaScript, I would recommend you to use a profiler, like the one built-in in Firebug:

alt text

Other alternatives can be the one included with Google Chrome, or IE8

If you want to do it programmatically, you could use Date objects to get a time difference:

var startTime = new Date();
// ...
// ...
var endTime = new Date();
var delta = endTime - startTime; // difference in milliseconds
CMS
Note: using the Date approach, expect the accuracy of the times to be within 15ms at best (on Windows, at least): http://ejohn.org/blog/accuracy-of-javascript-time/
Josh Stodola
Thanks Josh!, I was looking for that article!
CMS
You can expect the same accuracy issues in a firebug as you do in normal JavaScript execution. It also has the problem of an observer effect, slowing down your JavaScript because it runs in the same UI thread of the browser that the javascript does.
Annie
Firebug looks really cool. It's more than I need for now but I likely have use for it down the road. Thanks for the suggestion.
Rob Segal
+4  A: 

For VBScript you can use Timer:

StartTime = Timer()
EndTime = Timer()
Response.Write("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2))

Or ASP Profiler (that is for an ASP environment.)

For JavaScript you can use Date:

var start = new Date().getTime()
alert("Milliseconds: " + (new Date().getTime() - start))

Firebug also has a profiler for JavaScript.

jspcal
Wow there has been a great batch of replies here. I think the "StartTime = Timer() EndTime = Timer()" method will work the best for me. I don't need anything overly complicated or fine detailed and this works for my purposes. Thanks.
Rob Segal
A: 

This is a great article on JavaScript timing, from the book "Even Faster Websites". Two things to keep in mind are that JavaScript Date objects often have a resolution as big as 15ms (this varies by browser and operating system), and most profilers have an observer effect. Note that Google Speed Tracer (new since the article was published) runs out of process and collects data asynchronously to minimize the observer effect.

Annie