Hello,
I need to implement a method that can measure Time and CPU cycles of context switch between threads in Windows.
Here is my code
#include <stdio.h>
#include <windows.h>
#include <time.h>
LARGE_INTEGER initialTimeStamp, finalTimeStamp, freq;
DWORD ThreadProc(LPVOID lpdwThreadParam)
{
SwitchToThread();
return 0;
}
int main()
{
int result;
HANDLE hThread;
QueryPerformanceFrequency(&freq);
hThread = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ThreadProc,NULL, 0, 0);
QueryPerformanceCounter(&initialTimeStamp);
SwitchToThread();
QueryPerformanceCounter(&finalTimeStamp);
result = (1000000 * ((finalTimeStamp.QuadPart - initialTimeStamp.QuadPart) / 2 ) / freq.QuadPart);
printf("Windows Thread - context switch time is %u ns\n", result);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
Note: The division by two, is because I have two context switch between initialTimeStamp and finalTimeStamp.
I don't know if this is a best or correct way to do it... I get different time on each execution, which is not what I was expecting. I'm not sure how to get a number of CPU cycles.
Any help will be appreciated.