在VBScript中衡量代码执行时间的好方法是什么?
或者如何在JavaScript中失败?
答案 0 :(得分:37)
对于VBScript,您可以使用Timer:
StartTime = Timer()
EndTime = Timer()
Response.Write("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2))
或ASP Profiler(适用于ASP环境。)
对于JavaScript,您可以使用日期:
var start = new Date().getTime()
alert("Milliseconds: " + (new Date().getTime() - start))
Firebug还有一个JavaScript分析器。
答案 1 :(得分:4)
对于JavaScript,我建议您使用分析器,例如Firebug中内置的分析器:
(来源:getfirebug.com)
如果您想以编程方式执行此操作,可以使用Date
个对象来获取时差:
var startTime = new Date();
// ...
// ...
var endTime = new Date();
var delta = endTime - startTime; // difference in milliseconds
答案 2 :(得分:3)
在此处找到具有适当的小时/分钟/秒格式的完美功能: https://social.technet.microsoft.com/wiki/contents/articles/633.vbscript-determine-script-execution-time.aspx
<强>用法:强>
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
答案 3 :(得分:1)
对于JavaScript,请使用Firebug或IE个人资料工具,或免费DynaTrace AJAX edition profiler。
答案 4 :(得分:0)
这真的取决于你想要衡量的是什么?
如果正在测试某些东西,并想知道某些碎片走了多长时间,你可以只声明一个开始和停止时间变量,然后在它们之间做差异....
答案 5 :(得分:0)
d = new Date();
x = 0;
for (i = 0; i < 1e7; ++i) { x += i; }
d2 = new Date();
d2 - d
12296
使用Date
对象,自1970年1月1日起以毫秒为单位返回valueOf()
。然后,您可以减去时间以获得经过的时间(以毫秒为单位)。
答案 6 :(得分:0)
This是一篇关于JavaScript计时的精彩文章,来自“Even Faster Websites”一书。需要记住的两件事是,JavaScript Date对象的分辨率通常高达15ms(这取决于浏览器和操作系统),大多数分析器都有observer effect。请注意Google Speed Tracer(自文章发布以来的新版本)用完了流程并异步收集数据以最大限度地减少观察者的影响。