Answers:
除非您的功能很慢,否则您将需要一个非常高分辨率的计时器。我知道的最准确的是QueryPerformanceCounter
。谷歌获取更多信息。尝试推动以下到一类,把它CTimer
说,那么你可以让一个实例全球某处,只是打电话.StartCounter
和.TimeElapsed
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private m_CounterStart As LARGE_INTEGER
Private m_CounterEnd As LARGE_INTEGER
Private m_crFrequency As Double
Private Const TWO_32 = 4294967296# ' = 256# * 256# * 256# * 256#
Private Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
Low = LI.lowpart
If Low < 0 Then
Low = Low + TWO_32
End If
LI2Double = LI.highpart * TWO_32 + Low
End Function
Private Sub Class_Initialize()
Dim PerfFrequency As LARGE_INTEGER
QueryPerformanceFrequency PerfFrequency
m_crFrequency = LI2Double(PerfFrequency)
End Sub
Public Sub StartCounter()
QueryPerformanceCounter m_CounterStart
End Sub
Property Get TimeElapsed() As Double
Dim crStart As Double
Dim crStop As Double
QueryPerformanceCounter m_CounterEnd
crStart = LI2Double(m_CounterStart)
crStop = LI2Double(m_CounterEnd)
TimeElapsed = 1000# * (crStop - crStart) / m_crFrequency
End Property
TimeElapsed()
给出结果(以毫秒为单位)。我没有执行任何开销补偿,因为我更担心开销计算中的口吃而不是完美的准确性。
GetTickCount
从kernel32 导入)给出相同的结果。
StartCounter
And TimeElapsed
?我在一CTimer
开始就做了一个实例Timer,With StartCounter
我只是.StartCounter
在我的潜艇开始后写了.TimeElapsed
,它回答了我Invalid use of property
。当我更不用说.StartCounter
它告诉我未设置对象时。
Declare PtrSafe Function
stackoverflow.com/questions/21611744/…–
VBA中的计时器功能使您自午夜起经过的秒数达到1/100秒。
Dim t as single
t = Timer
'code
MsgBox Timer - t
如果您试图像秒表一样返回时间,则可以使用以下API,它返回自系统启动以来的时间(以毫秒为单位):
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub testTimer()
Dim t As Long
t = GetTickCount
For i = 1 To 1000000
a = a + 1
Next
MsgBox GetTickCount - t, , "Milliseconds"
End Sub
http://www.pcreview.co.uk/forums/grab-time-milliseconds-included-vba-t994765.html之后(由于winmm.dll中的timeGetTime对我不起作用,而QueryPerformanceCounter对于所需的任务来说太复杂了)
Public Declare Function ...
部分?它在矿井底部添加代码时创建了一个错误
对于新手,这些链接说明了如何对要计时的所有潜艇进行自动性能分析:
http://www.nullskull.com/a/1602/profiling-and-optimizing-vba.aspx
http://sites.mcpher.com/share/Home/excelquirks/optimizationlink 看到procProfiler.zip在http://sites.mcpher.com/share/Home/excelquirks/downlable-items
Sub Macro1()
Dim StartTime As Double
StartTime = Timer
''''''''''''''''''''
'Your Code'
''''''''''''''''''''
MsgBox "RunTime : " & Format((Timer - StartTime) / 86400, "hh:mm:ss")
End Sub
输出:
播放时间:00:00:02
多年来,我们一直使用基于winmm.dll中基于timeGetTime的解决方案来实现毫秒级精度。参见http://www.aboutvb.de/kom/artikel/komstopwatch.htm
这篇文章是用德语编写的,但是下载中的代码(包装dll函数调用的VBA类)很容易使用和理解,而无法阅读本文。
秒,含2个小数位:
Dim startTime As Single 'start timer
MsgBox ("run time: " & Format((Timer - startTime) / 1000000, "#,##0.00") & " seconds") 'end timer
毫秒:
Dim startTime As Single 'start timer
MsgBox ("run time: " & Format((Timer - startTime), "#,##0.00") & " milliseconds") 'end timer
使用逗号分隔符的毫秒数:
Dim startTime As Single 'start timer
MsgBox ("run time: " & Format((Timer - startTime) * 1000, "#,##0.00") & " milliseconds") 'end timer
只是将其留在这里,供任何正在寻找像我一样使用秒到2小数位格式的简单计时器的人使用。这些是我喜欢使用的简短而可爱的小计时器。它们在子或函数的开头仅占用一行代码,而在结尾处仅占用一行代码。这些并不意味着疯狂准确,我个人通常并不关心少于1/100秒的内容,但是毫秒计时器将为您提供这3种中最准确的运行时间。我也读过您如果它恰好在午夜时分运行时可能会得到不正确的读数,这种情况很少见,但仅供参考。