[SIZE=2][[/SIZE][SIZE=2][COLOR=#008080]DllImport[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"kernel32.dll"[/COLOR][/SIZE][SIZE=2])][/SIZE]
[SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]extern[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][SIZE=2] GetThreadTimes([/SIZE][SIZE=2][COLOR=#008080]IntPtr[/COLOR][/SIZE][SIZE=2] hThread, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]long[/COLOR][/SIZE][SIZE=2] lpCreationTime,[/SIZE]
[SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]long[/COLOR][/SIZE][SIZE=2] lpExitTime, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]long[/COLOR][/SIZE][SIZE=2] lpKernelTime, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]long[/COLOR][/SIZE][SIZE=2] lpUserTime);[/SIZE]
Как протестить реальную скорость работы приложения.
Проблема в том, что если для решения данной задачи использовать TickCount, то будет учитываться и время, которое тратится ну другие процессы и потоки в системе. Как найти время работы именно для данного потока или процесса(в частности конкретной прожки).
Может проги там какие есть? Подскажите. Единственное, что эти проги могут тоже работать по тому же принципу.
RTFM GetThreadTimes
GetThreadTimes (kernel32). Pinvoke
Retrieves timing information for the specified thread.
BOOL WINAPI GetThreadTimes(
HANDLE hThread,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);
[SIZE=2]Parameters[/SIZE]
hThread [in] A handle to the thread whose timing information is sought. This handle must have the THREAD_QUERY_INFORMATION access right. For more information, see [COLOR=#0000ff]Thread Security and Access Rights[/COLOR]. lpCreationTime [out] A pointer to a [COLOR=#0000ff]FILETIME[/COLOR] structure that receives the creation time of the thread. lpExitTime [out] A pointer to a FILETIME structure that receives the exit time of the thread. If the thread has not exited, the content of this structure is undefined. lpKernelTime [out] A pointer to a FILETIME structure that receives the amount of time that the thread has executed in kernel mode. lpUserTime [out] A pointer to a FILETIME structure that receives the amount of time that the thread has executed in user mode. [SIZE=2]Return Value[/SIZE]
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call [COLOR=#0000ff]GetLastError[/COLOR].
[SIZE=2]Remarks[/SIZE]
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.
Thread 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; see [COLOR=#0000ff]Time Functions[/COLOR].
Thread kernel mode and user mode times are amounts of time. For example, if a thread 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.
Пример:
В целом рекомендую использовать профайлеры, например JetBrains .NET Profiler
GetThreadTimes (kernel32). Pinvoke
Цитата:
Retrieves timing information for the specified thread.
BOOL WINAPI GetThreadTimes(
HANDLE hThread,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);
[SIZE=2]Parameters[/SIZE]
hThread [in] A handle to the thread whose timing information is sought. This handle must have the THREAD_QUERY_INFORMATION access right. For more information, see [COLOR=#0000ff]Thread Security and Access Rights[/COLOR]. lpCreationTime [out] A pointer to a [COLOR=#0000ff]FILETIME[/COLOR] structure that receives the creation time of the thread. lpExitTime [out] A pointer to a FILETIME structure that receives the exit time of the thread. If the thread has not exited, the content of this structure is undefined. lpKernelTime [out] A pointer to a FILETIME structure that receives the amount of time that the thread has executed in kernel mode. lpUserTime [out] A pointer to a FILETIME structure that receives the amount of time that the thread has executed in user mode. [SIZE=2]Return Value[/SIZE]
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call [COLOR=#0000ff]GetLastError[/COLOR].
[SIZE=2]Remarks[/SIZE]
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.
Thread 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; see [COLOR=#0000ff]Time Functions[/COLOR].
Thread kernel mode and user mode times are amounts of time. For example, if a thread 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.
C# Signature:
Код:
Пример:
Код:
[SIZE=2][COLOR=#0000ff] long[/COLOR][/SIZE][SIZE=2] l, KernelStart, UserStart;[/SIZE]
[SIZE=2] GetThreadTimes(GetCurrentThread(), [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] KernelStart, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] UserStart);[/SIZE]
[SIZE=2][COLOR=#0000ff] try[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2] FunctionToProfile(); [/SIZE][SIZE=2][COLOR=#008000]// Whatever function you want to measure CPU usage of.[/COLOR][/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] finally[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] long[/COLOR][/SIZE][SIZE=2] KernelEnd, UserEnd;[/SIZE]
[SIZE=2] GetThreadTimes(GetCurrentThread(), [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] KernelEnd, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] UserEnd);[/SIZE]
[SIZE=2][COLOR=#0000ff] long[/COLOR][/SIZE][SIZE=2] TimeUsed = (KernelEnd - KernelStart) + (UserEnd - UserStart);[/SIZE]
[SIZE=2][COLOR=#008080] Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Function used {0}ms CPU"[/COLOR][/SIZE][SIZE=2], TimeUsed / 10000.0);[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] GetThreadTimes(GetCurrentThread(), [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] KernelStart, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] UserStart);[/SIZE]
[SIZE=2][COLOR=#0000ff] try[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2] FunctionToProfile(); [/SIZE][SIZE=2][COLOR=#008000]// Whatever function you want to measure CPU usage of.[/COLOR][/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] finally[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] long[/COLOR][/SIZE][SIZE=2] KernelEnd, UserEnd;[/SIZE]
[SIZE=2] GetThreadTimes(GetCurrentThread(), [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] l, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] KernelEnd, [/SIZE][SIZE=2][COLOR=#0000ff]out[/COLOR][/SIZE][SIZE=2] UserEnd);[/SIZE]
[SIZE=2][COLOR=#0000ff] long[/COLOR][/SIZE][SIZE=2] TimeUsed = (KernelEnd - KernelStart) + (UserEnd - UserStart);[/SIZE]
[SIZE=2][COLOR=#008080] Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Function used {0}ms CPU"[/COLOR][/SIZE][SIZE=2], TimeUsed / 10000.0);[/SIZE]
[SIZE=2] }[/SIZE]
В целом рекомендую использовать профайлеры, например JetBrains .NET Profiler
Спасибо! Теперь все работает так как нужно.