How to get time elapsed in milliseconds

2020-04-03 12:44发布

Since performance of string concatenation is quite weak in VB6 I'm testing several StringBuilder implementations. To see how long they're running, I currently use the built-in

Timer

function which only gives me the number of seconds that have passed after midnight.

Is there a way (I guess by importing a system function) to get something with milliseconds precision?

8条回答
兄弟一词,经得起流年.
2楼-- · 2020-04-03 13:19

You can use two Win32 APIs:

These use LARGE_INTEGER to represent 64 bit numbers.

查看更多
太酷不给撩
3楼-- · 2020-04-03 13:24

I always use this in a module somewhere (could be in a class though). This code allows you to maintain up to six timers, with high accuracy:

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
查看更多
beautiful°
4楼-- · 2020-04-03 13:25

Put the following code in a Stopwatch class:

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

You can use it as follows:

Dim sw as StopWatch
sw = New StopWatch
sw.Start

' Code you want to time

Debug.Print "Code took " & sw.ElapsedMilliseconds " ms"
查看更多
叼着烟拽天下
5楼-- · 2020-04-03 13:29

There's a Thomas Edison story, where he's interviewing some prospective engineers.

He asks them to determine the volume of a light bulb. Candidate A measures it and then uses the formula for the volume of a sphere, and another formula for the volume of the neck, and so on. Candidate B fills it with water and pours it into a measuring cup. Who do you think got the job?

Run it 1000 times and look at your watch before and after. Seconds = milliseconds.

查看更多
家丑人穷心不美
6楼-- · 2020-04-03 13:30

Yes, you can use the Win32 API:

DWORD WINAPI GetTickCount(void);

To import it in VB6 declare it like this:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Call it before the operation and after and then calculate the difference in time passed.

查看更多
Viruses.
7楼-- · 2020-04-03 13:32

There's code and an explanation in the MSDN KB article Q172338 How To Use QueryPerformanceCounter to Time Code

查看更多
登录 后发表回答