如何获取经过的时间(以毫秒为单位)

发布于 2024-07-25 13:26:14 字数 174 浏览 6 评论 0原文

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

Timer

函数,它只提供午夜后经过的秒数。

有没有办法(我想通过导入系统函数)来获得毫秒精度的东西?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

痞味浪人 2024-08-01 13:26:15

是的,您可以使用Win32 API:

DWORD WINAPI GetTickCount(void);

要在VB6中导入它,请这样声明:

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

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

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.

谈情不如逗狗 2024-08-01 13:26:15

将以下代码放入 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
Set sw = New StopWatch
sw.Start

' Code you want to time

Debug.Print "Code took " & sw.ElapsedMilliseconds " ms"

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
Set sw = New StopWatch
sw.Start

' Code you want to time

Debug.Print "Code took " & sw.ElapsedMilliseconds " ms"
梦里兽 2024-08-01 13:26:15

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

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.

看透却不说透 2024-08-01 13:26:15

您可以使用两个 Win32 API:

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

You can use two Win32 APIs:

These use LARGE_INTEGER to represent 64 bit numbers.

一刻暧昧 2024-08-01 13:26:15

MSDN 知识库文章 Q172338 如何使用 QueryPerformanceCounter 来计时代码中提供了代码和说明

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

只为守护你 2024-08-01 13:26:15

有一个托马斯·爱迪生的故事,他正在面试一些未来的工程师。

他要求他们确定灯泡的体积。 考生 A 测量它,然后使用球体体积的公式和颈部体积的另一个公式,依此类推。 考生 B 将其装满水并倒入量杯中。 你认为谁得到了这份工作?

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

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.

眼睛会笑 2024-08-01 13:26:15

我总是在某个模块中使用它(不过也可以在一个类中)。 此代码允许您维护最多六个计时器,并且具有高精度:

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

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
空城缀染半城烟沙 2024-08-01 13:26:15

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

Imports System.Diagnostics

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

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

Imports System.Diagnostics

Dim sw As New Stopwatch()
sw.Start()
// do something
LOG("Elapsed " + sw.ElapsedMilliseconds + " ms")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文