你如何测试VBA代码的运行时间?在VBA中是否有代码可以包装一个函数,让我知道它运行的时间,以便我可以比较函数的不同运行时间?
3 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
除非你的功能很慢,否则你需要一个非常高分辨率的计时器。我所知道的最准确的是QueryPerformanceCounter
。谷歌更多信息。尝试推下到一个类,把它CTimer
说,那么你可以让一个实例全球某处,只是打电话.StartCounter
和.TimeElapsed
Option ExplicitPrivate Type LARGE_INTEGER lowpart As Long highpart As LongEnd TypePrivate Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As LongPrivate Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As LongPrivate m_CounterStart As LARGE_INTEGERPrivate m_CounterEnd As LARGE_INTEGERPrivate m_crFrequency As DoublePrivate Const TWO_32 = 4294967296# ' = 256# * 256# * 256# * 256#Private Function LI2Double(LI As LARGE_INTEGER) As DoubleDim Low As Double Low = LI.lowpart If Low < 0 Then Low = Low + TWO_32 End If LI2Double = LI.highpart * TWO_32 + LowEnd FunctionPrivate Sub Class_Initialize()Dim PerfFrequency As LARGE_INTEGER QueryPerformanceFrequency PerfFrequency m_crFrequency = LI2Double(PerfFrequency)End SubPublic Sub StartCounter() QueryPerformanceCounter m_CounterStartEnd SubProperty Get TimeElapsed() As DoubleDim crStart As DoubleDim crStop As Double QueryPerformanceCounter m_CounterEnd crStart = LI2Double(m_CounterStart) crStop = LI2Double(m_CounterEnd) TimeElapsed = 1000# * (crStop - crStart) / m_crFrequencyEnd Property
catspeake
TA贡献1111条经验 获得超0个赞
VBA中的定时器功能为您提供从午夜到1/100秒的经过秒数。
Dim t as single
t = Timer
'code
MsgBox Timer - t
添加回答
举报
0/150
提交
取消