- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在循环遍历每种数据类型(整数、 double 、十进制和变量)的 100 万个随机数后,我试图测试数据类型之间的执行时间差异。我从 Microsoft Developer 网站获取了这段代码。我正在使用 Excel 2010。
这是代码:
Option Explicit
Sub Function1()
Module Module1
Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Short
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As Long) As Short
Dim Ctr1, Ctr2, Freq As Long
Dim Acc, I As Integer
' Times 100 increment operations by using QueryPerformanceCounter.
If QueryPerformanceCounter(Ctr1) Then ' Begin timing.
For I = 1 To 100 ' Code is being timed.
Acc += 1
Next
QueryPerformanceCounter (Ctr2) ' Finish timing.
Console.WriteLine ("Start Value: " & Ctr1)
Console.WriteLine ("End Value: " & Ctr2)
QueryPerformanceFrequency (Freq)
Console.WriteLine ("QueryPerformanceCounter minimum resolution: 1/" & Freq & " seconds.")
Console.WriteLine ("100 Increment time: " & (Ctr2 - Ctr1) / Freq & " seconds.")
Else
Console.WriteLine ("High-resolution counter not supported.")
End If
'
' Keep console window open.
'
Console.WriteLine()
Console.Write ("Press ENTER to finish ... ")
Console.Read()
End Module
End Sub
Sub Function1_Int_RandNumCounter()
Dim Int_RandNum_X As Integer
Dim Int_RandNum_Y As Integer
Dim Count As Integer
For Count = 1 To Count = 1000000
Int_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Int_RandNum_Y = Rnd(Now)
Next Count
' Call Function1_Dbl_RandNumCounter
End Sub
Sub Function1_Dbl_RandNumCounter()
Dim Dbl_RandNum_X As Double, Dbl_RandNum_Y As Double, Count As Double
For Count = 1 To Count = 1000000
Dbl_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Dbl_RandNum_Y = Rnd(Now)
Next Count
Call Function1_Var_RandNumCounter
End Sub
Sub Function1_Var_RandNumCounter()
Dim Var_RandNum_X, Var_RandNum_Y, Count As Variant
For Count = 1 To Count = 1000000
Var_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Var_RandNum_Y = Rnd(Now)
Next Count
Call Function1_Dec_RandNumCounter
End Sub
Sub Function1_Dec_RandNumCounter()
Dim Count, Var_RandNum_X, dec_RandNum_X, Var_RandNum_Y, dec_RandNum_Y
dec_RandNum_X = CDec(Var_RandNum_X)
dec_RandNum_Y = CDec(Var_RandNum_Y) ' convert these vals to decimals
For Count = 1 To Count = 1000000
dec_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
dec_RandNum_Y = Rnd(Now)
Next Count
Call Function2_BarGraph
End Sub
Sub Function2_BarGraph()
' Put all of these vals in a 2D bar graph
End Sub
这段代码给了我错误,例如:
Compile error:
Only comments may appear after End Sub, End Function, or End Property
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private m_CounterStart As LARGE_INTEGER
Private m_CounterEnd As LARGE_INTEGER
Private m_crFrequency As Double
Private Const TWO_32 = 4294967296# ' = 256# * 256# * 256# * 256#
Private Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
Low = LI.lowpart
If Low < 0 Then
Low = Low + TWO_32
End If
LI2Double = LI.highpart * TWO_32 + Low
End Function
Private Sub Class_Initialize()
Dim PerfFrequency As LARGE_INTEGER
QueryPerformanceFrequency PerfFrequency
m_crFrequency = LI2Double(PerfFrequency)
End Sub
Public Sub StartCounter()
QueryPerformanceCounter m_CounterStart
End Sub
Property Get TimeElapsed() As Double
Dim crStart As Double
Dim crStop As Double
QueryPerformanceCounter m_CounterEnd
crStart = LI2Double(m_CounterStart)
crStop = LI2Double(m_CounterEnd)
TimeElapsed = 1000# * (crStop - crStart) / m_crFrequency
End Property
Sub Function1_Int_RandNumCounter()
Dim Int_RandNum_X As Integer
Dim Int_RandNum_Y As Integer
Dim Count As Integer
For Count = 1 To Count = 1000000
Int_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Int_RandNum_Y = Rnd(Now)
Next Count
' Call Function1_Dbl_RandNumCounter
End Sub
Sub Function1_Dbl_RandNumCounter()
Dim Dbl_RandNum_X As Double, Dbl_RandNum_Y As Double, Count As Double
For Count = 1 To Count = 1000000
Dbl_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Dbl_RandNum_Y = Rnd(Now)
Next Count
Call Function1_Var_RandNumCounter
End Sub
Sub Function1_Var_RandNumCounter()
Dim Var_RandNum_X, Var_RandNum_Y, Count As Variant
For Count = 1 To Count = 1000000
Var_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Var_RandNum_Y = Rnd(Now)
Next Count
Call Function1_Dec_RandNumCounter
End Sub
Sub Function1_Dec_RandNumCounter()
Dim Count, Var_RandNum_X, dec_RandNum_X, Var_RandNum_Y, dec_RandNum_Y
dec_RandNum_X = CDec(Var_RandNum_X)
dec_RandNum_Y = CDec(Var_RandNum_Y) ' convert these vals to decimals
For Count = 1 To Count = 1000000
dec_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
dec_RandNum_Y = Rnd(Now)
Next Count
Call Function2_BarGraph
End Sub
Sub Function2_BarGraph()
' Put all of these vals in a 2D bar graph
End Sub
编辑:新的 VBA 代码(我是否正确设置了此功能?)
Sub Function1_Int_RandNumCounter()
Dim Int_RandNum_X As Integer
Dim Int_RandNum_Y As Integer
Dim Count As Integer
Dim oPM As PerformanceMonitor
Dim Time_Int As Variant
Time_Int = CDec(Time_Int)
Set oPM = New PerformanceMonitor
oPM.StartCounter
For Count = 1 To Count = 1000000
Int_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
Int_RandNum_Y = Rnd(Now)
Next
Time_Int = oPM.TimeElapsed
' Call Function1_Dbl_RandNumCounter
End Sub
最佳答案
将一个新的类模块添加到您的项目中,将其命名为 PerformanceMonitor 并将此代码从我在评论中链接到的线程中粘贴到该类中:
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private m_CounterStart As LARGE_INTEGER
Private m_CounterEnd As LARGE_INTEGER
Private m_crFrequency As Double
Private Const TWO_32 = 4294967296# ' = 256# * 256# * 256# * 256#
Private Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
Low = LI.lowpart
If Low < 0 Then
Low = Low + TWO_32
End If
LI2Double = LI.highpart * TWO_32 + Low
End Function
Private Sub Class_Initialize()
Dim PerfFrequency As LARGE_INTEGER
QueryPerformanceFrequency PerfFrequency
m_crFrequency = LI2Double(PerfFrequency)
End Sub
Public Sub StartCounter()
QueryPerformanceCounter m_CounterStart
End Sub
Property Get TimeElapsed() As Double
Dim crStart As Double
Dim crStop As Double
QueryPerformanceCounter m_CounterEnd
crStart = LI2Double(m_CounterStart)
crStop = LI2Double(m_CounterEnd)
TimeElapsed = 1000# * (crStop - crStart) / m_crFrequency
End Property
StartCounter
方法在您想要计时的代码开头,然后在最后调用它的
TimeElapsed
属性以查看花费了多长时间(以毫秒为单位)。例如:
Sub foo()
Dim n As Long
Dim oPM As PerformanceMonitor
Set oPM = New PerformanceMonitor
oPM.StartCounter
For n = 1 To 100000
Debug.Print n
Next
MsgBox oPM.TimeElapsed
Set oPM = Nothing
End Sub
关于VBA QueryPerformanceCounter 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31383177/
在循环遍历每种数据类型(整数、 double 、十进制和变量)的 100 万个随机数后,我试图测试数据类型之间的执行时间差异。我从 Microsoft Developer 网站获取了这段代码。我正在使
嘿,我正在使用 QueryPerformanceCounter 来计算该函数花费的时间(以毫秒为单位),但我收到了这个运行时错误: Run-Time Check Failure #2 - Stack
我正在使用 QueryPerformanceCounter 来测量某些函数/操作的时间。它曾经给我正确的数字,例如,我可以测试 Sleep(1000),它会返回非常接近 1 秒的时间。现在,它返回一个
我想使用 PerformanceCounter 来衡量执行某些操作需要多少时间。 我对 PerformanceCounter 和 C++ 了解不多。我在这里找到了一些代码: How to use Qu
我需要衡量我编写的函数的性能。由于我不能使用 C++11 并且我需要微秒,所以我使用了 windows.h 中的 QueryPerformanceCounter。 #include LARGE_IN
假设从 QueryPerformanceCounter 返回的计数是否安全?与上次系统启动后的时间有关?还是可以在系统运行时重置? MSDN 文章本身并不能保证这一点,但是我看到一些第 3 方信息(例
我正在使用 QueryPerformanceCounter 在我的应用程序中做一些计时。但是,在运行几天后,该应用程序似乎停止正常运行。如果我只是重新启动应用程序,它就会再次开始工作。这让我相信我的计
在 Windows 中通过分辨率较低的计时方法调用 QueryPerformanceCounter 是否会对性能产生影响? 最佳答案 虽然老了,this Dr Dobb's article很好地总结了
我正在考虑在两个同时处于事件状态的线程中重复调用(旋转)QueryPerformanceCounter。我不确定这是否真的是一个问题,因为我没有看到任何关于它的文章,但是 QueryPerforman
我创建了一个简单的程序来确定 QueryPerformanceCounter 调用需要多长时间。在我的计算机上大约需要 8 纳秒,但有时我会看到每次调用出现高达 500 微秒的峰值。 一些细节: Vi
在 linux 中是否有一个等效的 C 函数来读取 CPU 计数器及其频率? 我正在寻找类似于 QueryPerformanceCounter 函数的东西,它可以读取现代 CPU 中的 64 位计数器
嗨,我正在使用 QueryperformanceCounter 对 Delphi 中的一段代码进行计时。由于某种原因,我使用 QueryPerformanceCounter 得到的毫秒数与使用秒表得到
我需要使用 QueryPerformanceCounter Win32 API 格式化日期时间。格式为: HH:mm:ss.ffffff ,包含时分秒和微秒。我需要使用这个函数,因为另一个进程(用 C
#include "TIMER1.h" #include "MAIN.h" typedef unsigned _int64 uint64; void TASK1() { uint64 freq, s
我有一个等待 std::condition_variable 然后循环直到它完成的线程。 我试图滑动我在 opengl 中绘制的矩形。 在不使用 delta 的情况下一切正常,但我希望无论在哪台计算机
我想知道 Windows 函数 GetSystemTimePreciseAsFileTime 是否使用 QueryPerformanceCounter/RDTSC 计数器以亚微秒分辨率检索 UTC 时
我已经对这里找到的 PRIMATEs 密码做了一些切片实现:http://primates.ae/ (我用的是 120 位版本)。 我只用 C 语言制作并使用了 Intel Intrinsics,这样
我在 this post 中找到了有关使用 QueryPerformanceCounter 的有用信息但我面临一个我还没有找到答案的问题。 我正在为 Windows CE 6.0 开发一个应用程序,需
我正在研究在我们的系统中使用 QueryPerformanceCounter 的确切含义,并试图了解它对应用程序的影响。从我的 4 核单 CPU 机器上运行它可以看出,它需要大约 230ns 才能运行
鉴于 C# 中的 Stopwatch 类可以在下面使用类似三个不同计时器的东西,例如 系统定时器精度约为 +-10 ms,具体取决于可以使用 timeBeginPeriod 设置的计时器分辨率它可能约
我是一名优秀的程序员,十分优秀!