Skip to content

Instantly share code, notes, and snippets.

@chaelim
Created April 21, 2013 19:18
Show Gist options
  • Save chaelim/5430711 to your computer and use it in GitHub Desktop.
Save chaelim/5430711 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <intrin.h>
#define DO_FLUSH
unsigned __stdcall thread(void* p)
{
unsigned __int64 t1 = __rdtsc();
volatile __int64 data = 0;
unsigned __int64 const count = 1000000000;
for (unsigned __int64 i = 0; i != count; ++i)
{
data *= data;
}
unsigned __int64 t2 = __rdtsc();
printf("time=%u\n", (unsigned)((t2-t1)*1000/count));
return 0;
}
int main()
{
HANDLE t = (HANDLE)_beginthreadex(0, 0, thread, 0,CREATE_SUSPENDED, 0);
SetThreadAffinityMask(t, 2);
SetThreadAffinityMask(GetCurrentThread(), 1);
ResumeThread(t);
unsigned __int64 tmin = 1000000000;
unsigned __int64 tmax = 0;
unsigned __int64 tsum = 0;
unsigned __int64 tcount = 0;
while (WAIT_TIMEOUT == WaitForSingleObject(t, 0))
{
#ifdef DO_FLUSH
unsigned __int64 t1 = __rdtsc();
FlushProcessWriteBuffers();
unsigned __int64 t2 = __rdtsc() - t1;
if (t2 < tmin)
tmin = t2;
if (t2 > tmax)
tmax = t2;
tsum += t2;
tcount += 1;
#endif
}
printf("min=%u, max=%u, mean=%d, count=%d\n", (unsigned)tmin,
(unsigned)tmax, tcount ? (unsigned)(tsum / tcount) : 0,
(unsigned)tcount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment