由于VB6中字符串连接的性能非常弱,我正在测试几个StringBuilder实现。要查看它们运行了多长时间,我目前使用内置的
Timer
这个函数只给我午夜后经过的秒数。
有没有办法(我猜通过导入系统函数)来获得毫秒精度的东西?
答案 0 :(得分:12)
是的,您可以使用Win32 API:
DWORD WINAPI GetTickCount(void);
要在VB6中导入它,请按以下方式声明:
Private Declare Function GetTickCount Lib "kernel32" () As Long
在操作之前和之后调用它,然后计算传递的时间差。
答案 1 :(得分:9)
将以下代码放入Stopwatch类:
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "Kernel32" (X As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "Kernel32" (X As Currency) As Boolean
Private m_startTime As Currency
Private m_freq As Currency
Private m_overhead As Currency
Public Sub start()
QueryPerformanceCounter m_startTime
End Sub
Public Function ElapsedSeconds() As Double
Dim currentTime As Currency
QueryPerformanceCounter currentTime
ElapsedSeconds = (currentTime - m_startTime - m_overhead) / m_freq
End Function
Public Function ElapsedMilliseconds() As Double
ElapsedMilliseconds = ElapsedSeconds * 1000
End Function
Private Sub Class_Initialize()
QueryPerformanceFrequency m_freq
Dim ctr1 As Currency
Dim ctr2 As Currency
QueryPerformanceCounter ctr1
QueryPerformanceCounter ctr2
m_overhead = ctr2 - ctr1
End Sub
您可以按如下方式使用它:
Dim sw as StopWatch
sw = New StopWatch
sw.Start
' Code you want to time
Debug.Print "Code took " & sw.ElapsedMilliseconds " ms"
答案 2 :(得分:2)
您也可以考虑使用其他方法。尝试从具有足够迭代次数的循环中调用您的例程,以便为您提供可测量的时间差。
答案 3 :(得分:1)
您可以使用两个Win32 API:
这些使用LARGE_INTEGER来表示64位数字。
答案 4 :(得分:1)
MSDN知识库文章Q172338 How To Use QueryPerformanceCounter to Time Code
中有代码和说明答案 5 :(得分:1)
有一个托马斯爱迪生的故事,他正在采访一些未来的工程师。
他要求他们确定灯泡的体积。候选人A对其进行测量,然后使用球体体积的公式,以及颈部体积的另一个公式,依此类推。候选人B用水填充并倒入量杯中。你认为谁得到了这份工作?
运行1000次,然后查看手表。秒=毫秒。
答案 6 :(得分:0)
我总是在某个模块中使用它(虽然可能在一个类中)。此代码允许您以高精度维护多达六个定时器:
Option Explicit
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private cFrequency As Currency
Private cCounters(0 To 5) As Currency
Public Sub StartCounter(Optional lCounterIndex As Long)
QueryPerformanceFrequency cFrequency
QueryPerformanceCounter cCounters(lCounterIndex)
End Sub
Public Function GetCounter(Optional lCounterIndex As Long) As Double
Dim cCount As Currency
QueryPerformanceFrequency cFrequency
QueryPerformanceCounter cCount
GetCounter = Format$((cCount - cCounters(lCounterIndex) - CCur(0.0008)) / cFrequency, "0.000000000")
End Function
Public Function Scientific(ByVal dValue As Double) As String
Dim lMultiplier As Long
Dim vNames As Variant
lMultiplier = 5
vNames = Array("peta", "tera", "giga", "mega", "kilo", "", "milli", "micro", "nano", "pico", "femto")
If Abs(dValue) < 1 Then
While Abs(dValue) < 1
dValue = dValue * 1000
lMultiplier = lMultiplier + 1
Wend
ElseIf Abs(dValue) >= 1000 Then
While Abs(dValue) >= 1000
dValue = dValue / 1000
lMultiplier = lMultiplier - 1
Wend
End If
Scientific = Format$(dValue, "0.000") & " " & vNames(lMultiplier)
End Function
答案 7 :(得分:-2)
您可以尝试使用System :: Diagnostics :: Stopwatch
Imports System.Diagnostics
Dim sw As New Stopwatch()
sw.Start()
// do something
LOG("Elapsed " + sw.ElapsedMilliseconds + " ms")