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

cxkjwg 2006-02-09 11:38:50
VB中,如何能计算出一个程序运行所需时间,要求精确到微秒.我现在用TIME函数只能精确到秒.
...全文
1103 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资源

7,763

社区成员

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

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