如何以毫秒为单位获得时间

时间:2022-04-25 01:27:16

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

由于字符串连接的性能在VB6中非常弱,我正在测试几个StringBuilder实现。为了了解它们运行了多长时间,我目前使用内置功能

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 个解决方案

#1


Yes, you can use the Win32 API:

是的,您可以使用Win32 API:

DWORD WINAPI GetTickCount(void);

To import it in VB6 declare it like this:

要在VB6中导入它,请按以下方式声明:

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

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

在操作之前和之后调用它然后计算通过的时间差。

#2


Put the following code in a Stopwatch class:

将以下代码放在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

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"

#3


You might also consider using a different approach. Try calling your routines from a loop with enough iterations to give you a measurable time difference.

您也可以考虑使用不同的方法。尝试从具有足够迭代次数的循环中调用您的例程,以便为您提供可测量的时间差。

#4


You can use two Win32 APIs:

您可以使用两个Win32 API:

  • QueryPerformanceCounter: To get the count at the start and end of the event.
  • QueryPerformanceCounter:获取事件开始和结束时的计数。

  • QueryPerformanceFrequency: To get the number of ticks per second.
  • QueryPerformanceFrequency:获取每秒的滴答数。

These use LARGE_INTEGER to represent 64 bit numbers.

它们使用LARGE_INTEGER来表示64位数。

#5


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

MSDN知识库文章Q172338中的代码和解释如何使用QueryPerformanceCounter来计时代码

#6


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?

他要求他们确定灯泡的体积。候选人A对其进行测量,然后使用球体体积的公式,以及颈部体积的另一个公式,依此类推。候选人B用水填充并倒入量杯中。你认为谁得到了这份工作?

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

运行1000次,然后看看你的手表。秒=毫秒。

#7


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

#8


You can try using the System::Diagnostics::Stopwatch

您可以尝试使用System :: Diagnostics :: Stopwatch

Imports System.Diagnostics

Dim sw As New Stopwatch()
sw.Start()
// do something
LOG("Elapsed " + sw.ElapsedMilliseconds + " ms")

#1


Yes, you can use the Win32 API:

是的,您可以使用Win32 API:

DWORD WINAPI GetTickCount(void);

To import it in VB6 declare it like this:

要在VB6中导入它,请按以下方式声明:

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

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

在操作之前和之后调用它然后计算通过的时间差。

#2


Put the following code in a Stopwatch class:

将以下代码放在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

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"

#3


You might also consider using a different approach. Try calling your routines from a loop with enough iterations to give you a measurable time difference.

您也可以考虑使用不同的方法。尝试从具有足够迭代次数的循环中调用您的例程,以便为您提供可测量的时间差。

#4


You can use two Win32 APIs:

您可以使用两个Win32 API:

  • QueryPerformanceCounter: To get the count at the start and end of the event.
  • QueryPerformanceCounter:获取事件开始和结束时的计数。

  • QueryPerformanceFrequency: To get the number of ticks per second.
  • QueryPerformanceFrequency:获取每秒的滴答数。

These use LARGE_INTEGER to represent 64 bit numbers.

它们使用LARGE_INTEGER来表示64位数。

#5


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

MSDN知识库文章Q172338中的代码和解释如何使用QueryPerformanceCounter来计时代码

#6


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?

他要求他们确定灯泡的体积。候选人A对其进行测量,然后使用球体体积的公式,以及颈部体积的另一个公式,依此类推。候选人B用水填充并倒入量杯中。你认为谁得到了这份工作?

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

运行1000次,然后看看你的手表。秒=毫秒。

#7


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

#8


You can try using the System::Diagnostics::Stopwatch

您可以尝试使用System :: Diagnostics :: Stopwatch

Imports System.Diagnostics

Dim sw As New Stopwatch()
sw.Start()
// do something
LOG("Elapsed " + sw.ElapsedMilliseconds + " ms")