如何测量 VBScript 或 JavaScript 中的代码执行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1985822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure code execution time in VBScript or JavaScript?
提问by Rob Segal
What is a good way to measure code execution time in VBScript?
在VBScript 中测量代码执行时间的好方法是什么?
Or failing that how to do it in JavaScript?
或者失败了如何在JavaScript 中做到这一点?
回答by jspcal
For VBScript you can use Timer:
对于 VBScript,您可以使用计时器:
StartTime = Timer()
EndTime = Timer()
Response.Write("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2))
Or ASP Profiler(that is for an ASP environment.)
或ASP Profiler(用于 ASP 环境。)
For JavaScript you can use Date:
对于 JavaScript,您可以使用日期:
var start = new Date().getTime()
alert("Milliseconds: " + (new Date().getTime() - start))
Firebugalso has a profiler for JavaScript.
Firebug也有一个用于 JavaScript 的分析器。
回答by CMS
For JavaScript, I would recommend you to use a profiler, like the one built-in in Firebug:
对于 JavaScript,我建议您使用分析器,例如Firebug 中内置的分析器:

(source: getfirebug.com)

(来源:getfirebug.com)
Other alternatives can be the one included with Google Chrome, or IE8
其他替代方案可以是 Google Chrome或IE8 中包含的替代方案
If you want to do it programmatically, you could use Dateobjects to get a time difference:
如果要以编程方式执行此操作,可以使用Date对象来获取时差:
var startTime = new Date();
// ...
// ...
var endTime = new Date();
var delta = endTime - startTime; // difference in milliseconds
回答by Concept211
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
在此处找到具有正确小时/分钟/秒格式的完美功能: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
回答by Craig Stuntz
For JavaScript, use the Firebugor IEprofilers, or free DynaTrace AJAX edition profiler.
对于 JavaScript,请使用Firebug或IE分析器,或免费的DynaTrace AJAX 版分析器。
回答by Mitchel Sellers
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....
如果正在测试某些东西并想知道某些片段持续了多长时间,您可以声明一个开始和停止时间变量,然后在它们之间进行区分....
回答by Jason S
d = new Date();
x = 0;
for (i = 0; i < 1e7; ++i) { x += i; }
d2 = new Date();
d2 - d
12296
Use the Dateobject, which returns a valueOf()in milliseconds since Jan 1 1970. You can then subtract times to get elapsed time in milliseconds.
使用自 1970 年 1 月 1 日以来Date返回valueOf()以毫秒为单位的对象。然后您可以减去时间以获得以毫秒为单位的经过时间。
回答by Annie
Thisis 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.
这是一篇关于 JavaScript 计时的精彩文章,出自“更快的网站”一书。要记住的两件事是 JavaScript Date 对象的分辨率通常高达 15 毫秒(这因浏览器和操作系统而异),并且大多数分析器具有观察者效应。请注意,Google Speed Tracer(自文章发表后新增)会耗尽进程并异步收集数据以最小化观察者效应。

