VBScript: Determine Script Execution Time
To output a script's execution time, include this at the beginning:
dtmStartTime = Timer
And include this at the end:
Wscript.Echo "Script execution time: " & Round(Timer - dtmStartTime, 2) & " seconds"
The Timer function returns the number of seconds that have elapsed since midnight (12:00 a.m.). The Round function controls the number of decimal places displayed.
Here is a function that will convert seconds to days, hours, and minutes.
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
Here is a sample script using the function that prints "Hello World!" to the screen, waits for 1 second, then outputs the script execution time.
dtmStartTime = Timer
Wscript.Echo "Hello, World!"
Wscript.Sleep 1000
Wscript.Echo "Script completed in " & GetElapsedTime
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