Skip to content

Commit

Permalink
ENH: allow getcpu to work on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xoviat committed Dec 23, 2017
1 parent cc17c56 commit 4fba341
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/scutil/cpu-stopwatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* since the most recent call. Very much not thread-safe.
*/

#ifndef _WIN32

#include <sys/times.h>
#include <unistd.h>
#include "scutil.h"
Expand Down Expand Up @@ -51,3 +53,33 @@ getcpu(void)
last = now;
return elapsed;
}

#else

#include <Windows.h>
#include "scutil.h"

unsigned long
getcpu(void)
{
LARGE_INTEGER ticks_per_second = -1;
LARGE_INTEGER ticks;

unsigned long last = 0;
unsigned long now, elapsed;

/* Initialize ticks_per_second. */
if (ticks_per_second.QuadPart <= 0)
QueryPerformanceFrequency(&ticks_per_second.QuadPart);

QueryPerformanceCounter(&ticks);
now = ticks.QuadPart;
now *= 1000; /* milliseconds */
now /= ticks_per_second.QuadPart;

elapsed = now - last;
last = now;
return elapsed;
}

#endif

0 comments on commit 4fba341

Please sign in to comment.