Rob Segal
Rob Segal

Reputation: 7625

How to measure code execution time in VBScript or JavaScript?

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

Or failing that how to do it in JavaScript?

Upvotes: 19

Views: 46605

Answers (7)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827744

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

alt text
(source: getfirebug.com)

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

Upvotes: 4

Concept211
Concept211

Reputation: 1076

Found the perfect function with proper Hours/Mins/Secs formatting here: https://social.technet.microsoft.com/wiki/contents/articles/633.vbscript-determine-script-execution-time.aspx

Usage:

dtmStartTime = Timer
Wscript.Echo "Hello, World!"
Wscript.Sleep 1000
Wscript.Echo "Script completed in " & GetElapsedTime

Function:

Function GetElapsedTime
    Const SECONDS_IN_DAY    = 86400
    Const SECONDS_IN_HOUR   = 3600
    Const SECONDS_IN_MINUTE = 60
    Const SECONDS_IN_WEEK   = 604800

    dtmEndTime = Timer

    seconds = Round(dtmEndTime - dtmStartTime, 2)
    If seconds < SECONDS_IN_MINUTE Then
        GetElapsedTime = seconds & " seconds "
        Exit Function
    End If
    If seconds < SECONDS_IN_HOUR Then 
        minutes = seconds / SECONDS_IN_MINUTE
        seconds = seconds MOD SECONDS_IN_MINUTE
        GetElapsedTime = Int(minutes) & " minutes " & seconds & " seconds "
        Exit Function
    End If
    If seconds < SECONDS_IN_DAY Then
        hours   = seconds / SECONDS_IN_HOUR
        minutes = (seconds MOD SECONDS_IN_HOUR) / SECONDS_IN_MINUTE
        seconds = (seconds MOD SECONDS_IN_HOUR) MOD SECONDS_IN_MINUTE
        GetElapsedTime = Int(hours) & " hours " & Int(minutes) & " minutes " & seconds & " seconds "
        Exit Function
    End If
    If seconds < SECONDS_IN_WEEK Then
        days    = seconds / SECONDS_IN_DAY
        hours   = (seconds MOD SECONDS_IN_DAY) / SECONDS_IN_HOUR
        minutes = ((seconds MOD SECONDS_IN_DAY) MOD SECONDS_IN_HOUR) / SECONDS_IN_MINUTE
        seconds = ((seconds MOD SECONDS_IN_DAY) MOD SECONDS_IN_HOUR) MOD SECONDS_IN_MINUTE
        GetElapsedTime = Int(days) & " days " & Int(hours) & " hours " & Int(minutes) & " minutes " & seconds & " seconds "
        Exit Function
    End If
End Function

Upvotes: 3

Jason S
Jason S

Reputation: 189826

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.

Upvotes: 0

Annie
Annie

Reputation: 6631

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.

Upvotes: 0

jspcal
jspcal

Reputation: 51914

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.

Upvotes: 43

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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....

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126587

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

Upvotes: 1

Related Questions