gpt4 book ai didi

excel - VBA 代码基准测试

转载 作者:行者123 更新时间:2023-12-01 17:57:44 25 4
gpt4 key购买 nike

对 VBA 代码进行基准测试最准确的方法是什么(在我的例子中,我在 Excel 中测试代码)?除了下面的 2 种之外,还有其他方法可以对代码进行基准测试吗?如果有,该方法的优点/缺点是什么?

这里有两种流行的方法。

第一个:计时器

Sub TimerBenchmark()

Dim benchmark As Double
benchmark = Timer

'Do your code here

MsgBox Timer - benchmark

End Sub

勾选(我认为这是最准确的):

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

Sub TickBenchmark()

Dim Start As Long
Dim Finish As Long

Start = GetTickCount()

'Do your code here

Finish = GetTickCount()
MsgBox CStr((Finish - Start) / 1000)

End Sub

最佳答案

以下代码使用比 Excel 更准确的 Windows 函数。取自http://msdn.microsoft.com/en-us/library/aa730921.aspx#Office2007excelPerf_MakingWorkbooksCalculateFaster 。同一页面还包含一些有关提高 Excel 2007 性能的重要提示。

Private Declare Function getFrequency Lib "kernel32" _
Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
Private Declare Function getTickCount Lib "kernel32" _
Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long

Function MicroTimer() As Double

'Returns seconds.

Dim cyTicks1 As Currency
Static cyFrequency As Currency
MicroTimer = 0

' Get frequency.
If cyFrequency = 0 Then getFrequency cyFrequency

' Get ticks.
getTickCount cyTicks1

' Seconds
If cyFrequency Then MicroTimer = cyTicks1 / cyFrequency
End Function

关于excel - VBA 代码基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7103552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com