Benchmarking VBA Code

后端 未结 4 1377
日久生厌
日久生厌 2020-11-29 01:12

What is considered the most accurate way to benchmark VBA code (in my case, I am testing code in Excel)? Are there any other techniques for benchmarking code besides the 2 b

4条回答
  •  北海茫月
    2020-11-29 02:01

    The following code uses a windows function that is more accurate than Excel. It is taken from http://msdn.microsoft.com/en-us/library/aa730921.aspx#Office2007excelPerf_MakingWorkbooksCalculateFaster. The same page also contains some great tips on improving performance in 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
    

提交回复
热议问题