VB中,如何能计算出一个程序运行所需时间,要求精确到微秒.我现在用TIME函数只能精确到秒.

cxkjwg 2006-02-09 11:38:50
VB中,如何能计算出一个程序运行所需时间,要求精确到微秒.我现在用TIME函数只能精确到秒.
...全文
1185 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xDAVIDx 2006-02-09
  • 打赏
  • 举报
回复
试试timeGettime函数吧 :)
xtuwz 2006-02-09
  • 打赏
  • 举报
回复
你可以从http://www.mvps.org/ccrp/controls/ccrptimer6.htm下载免费的CCRP High Performance Timer Objects控件。VB的Timer控件实际精度大约55ms,这个控件大约1ms(基本是目前Windows能够达到的极限了)。如果你要进行延时,可以使用API函数timeSetEvent,它可以实现1ms的精度。我在网上看的,希望对你有帮助.
zyl910 2006-02-09
  • 打赏
  • 举报
回复
若是检查某段代码的运行时间,用QueryPerformanceCounter、QueryPerformanceFrequency

'Performance Counter
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 Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim T As Long, liFrequency As LARGE_INTEGER, liStart As LARGE_INTEGER, liStop As LARGE_INTEGER
Dim cuFrequency As Currency, cuStart As Currency, cuStop As Currency
'Retrieve the frequency of the performance counter
If QueryPerformanceFrequency(liFrequency) = 0 Then
MsgBox "Your hardware doesn't support a high-resolution performance counter!", vbInformation
Else
'convert the large integer to currency
cuFrequency = LargeIntToCurrency(liFrequency)
'retrieve tick count
QueryPerformanceCounter liStart
'do something
For T = 0 To 100000
DoEvents
Next T
'retrieve tick count
QueryPerformanceCounter liStop
'convert large integers to currency's
cuStart = LargeIntToCurrency(liStart)
cuStop = LargeIntToCurrency(liStop)
'calculate how many seconds passed, and show the result
MsgBox "Time: " + CStr((cuStop - cuStart) / cuFrequency) + " seconds"
End If
End Sub
Private Function LargeIntToCurrency(liInput As LARGE_INTEGER) As Currency
'copy 8 bytes from the large integer to an ampty currency
CopyMemory LargeIntToCurrency, liInput, LenB(liInput)
'adjust it
LargeIntToCurrency = LargeIntToCurrency * 10000
End Function
zyl910 2006-02-09
  • 打赏
  • 举报
回复
GetProcessTimes是得到某个进程的运行时间,精确到毫秒
1.用OpenProcess打开进程
2.用GetProcessTimes得到进程时间
3.用FileTimeToSystemTime将FILETIME转为SYSTEMTIME
4.用CloseHandle关闭进程句柄
cxkjwg 2006-02-09
  • 打赏
  • 举报
回复
to:zyl910(910:分儿,我又来了!) ,有没有啥子简单的办法,能否举个例子,谢谢
cxkjwg 2006-02-09
  • 打赏
  • 举报
回复
有個API函數的。
叫什麽忘了,你自己查查。,

能否说具体点?拜托
vansoft 2006-02-09
  • 打赏
  • 举报
回复
有個API函數的。
叫什麽忘了,你自己查查。
zyl910 2006-02-09
  • 打赏
  • 举报
回复
GetProcessTimes

The GetProcessTimes function retrieves timing information for the specified process.


BOOL GetProcessTimes(
HANDLE hProcess,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);

Parameters
hProcess
[in] Handle to the process whose timing information is sought. This handle must be created with the PROCESS_QUERY_INFORMATION access right. For more information, see Process Security and Access Rights.
lpCreationTime
[out] Pointer to a FILETIME structure that receives the creation time of the process.
lpExitTime
[out] Pointer to a FILETIME structure that receives the exit time of the process. If the process has not exited, the content of this structure is undefined.
lpKernelTime
[out] Pointer to a FILETIME structure that receives the amount of time that the process has executed in kernel mode. The time that each of the threads of the process has executed in kernel mode is determined, and then all of those times are summed together to obtain this value.
lpUserTime
[out] Pointer to a FILETIME structure that receives the amount of time that the process has executed in user mode. The time that each of the threads of the process has executed in user mode is determined, and then all of those times are summed together to obtain this value.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
All times are expressed using FILETIME data structures. Such a structure contains two 32-bit values that combine to form a 64-bit count of 100-nanosecond time units.

Process creation and exit times are points in time expressed as the amount of time that has elapsed since midnight on January 1, 1601 at Greenwich, England. There are several functions that an application can use to convert such values to more generally useful forms.

Process kernel mode and user mode times are amounts of time. For example, if a process has spent one second in kernel mode, this function will fill the FILETIME structure specified by lpKernelTime with a 64-bit value of ten million. That is the number of 100-nanosecond units in one second.

Requirements
Client Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 3.5 and later.
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 3.5 and later.
Header Declared in Winbase.h; include Windows.h.

Library Link to Kernel32.lib.

DLL Requires Kernel32.dll.
faysky2 2006-02-09
  • 打赏
  • 举报
回复
可以用GetTickCount API函数

'计算窗体运行的时间(单位为毫秒)
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim bTime As Long, eTime As Long

Private Sub Form_Load()
bTime = GetTickCount
End Sub

Private Sub Form_Unload(Cancel As Integer)
eTime = GetTickCount
MsgBox "窗体运行了" & eTime - bTime & "毫秒"
End Sub
TroubleShotting 2006-02-09
  • 打赏
  • 举报
回复
同zyl910的
用两个API,QueryPerformanceCounter,QueryPerformanceFrequency
原理是这两个函数计算程序运行时经历了多少个震荡周期
应用中感觉到这个方法比其他的好
chenjiumei 2006-02-09
  • 打赏
  • 举报
回复
我也遇到同样的问题,不过发现,用TIMER控件可以解决.具体操作如下:
在窗体中,加入TIMER控件,在程序的开头,设置一个字符变量,记录开始时的系统时间,在程序的结尾,设置一个字符变量,记录结束时间,再设置一个 Double变量,计算结束时间与开始时间的差即可.举例:
dim begin_time as string
begin_time=timer
......(此部分为程序主体部分)
dim end_time as string
end_time=timer
dim operator_time as Double
operator_time=cdbl(end_time)-cdbl(begin_time)
小数点前表示秒.
希望能给你帮助
zyl910 2006-02-09
  • 打赏
  • 举报
回复
timeGettime的实现原理:创建一个高优先级的线程反复调用QueryPerformanceCounter、QueryPerformanceFrequency检查时间
严重占用CPU资源
内容概要:本文围绕“单相逆变器闭环逆变电路PWM模型仿真研究”展开,基于Simulink平台构建单相逆变器的闭环控制系统仿真模型,重点研究PWM调制技术在逆变电路的应用与实现。文详细阐述了系统架构设计、电压电流双闭环控制策略的实现原理、控制器参数设计及仿真建模全过程,并通过仿真结果验证了控制方案在动态响应、稳态精度与系统稳定性方面的有效性。同时,文档还涵盖多种电力电子系统典型应用场景,如多类型短路故障仿真(性点不接地、经小电阻接地、经消弧线圈接地等)、软开关技术、微电网能量管理、MPPT控制等,体现较强的技术综合性和工程实践价值。; 适合人群:电气工程、自动化、电力电子与新能源等相关专业的高校本科生、研究生、科研人员,以及从事电力系统仿真、逆变器设计与新能源并网技术研发的工程技术人员。; 使用场景及目标:①掌握基于Simulink的单相逆变器闭环控制系统建模与PWM仿真方法;②深入理解双闭环控制、SPWM/SVPWM调制、系统稳定性分析等核心技术原理;③为课程设计、毕业设计、科研项目或实际工程开发提供可复用的仿真模型与技术支持; 阅读建议:建议结合文仿真模型动手实践,重点掌握PI控制器参数整定、PWM信号生成机制与仿真结果分析方法,同时可延伸学习文档涉及的软开关、故障仿真、微电网控制等关联技术,以拓展系统级设计能力。

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧