Source

line_profiler_non_selective / timers.c

Full commit
#include "Python.h"

/* The following timer code comes from Python 2.5.2's _lsprof.c */

#if !defined(HAVE_LONG_LONG)
#error "This module requires long longs!"
#endif

/*** Selection of a high-precision timer ***/

#ifdef MS_WINDOWS

#include <windows.h>

PY_LONG_LONG
hpTimer(void)
{
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return li.QuadPart;
}

double
hpTimerUnit(void)
{
        LARGE_INTEGER li;
        if (QueryPerformanceFrequency(&li))
                return 1.0 / li.QuadPart;
        else
                return 0.000001;  /* unlikely */
}

#else  /* !MS_WINDOWS */

#ifndef HAVE_GETTIMEOFDAY
#error "This module requires gettimeofday() on non-Windows platforms!"
#endif

#include <sys/time.h>
#include <time.h>

#define MY_CLOCK CLOCK_MONOTONIC

PY_LONG_LONG
hpTimer(void)
{
    struct timespec tp;
    long err = clock_gettime(MY_CLOCK, &tp);
    if (err)
        return 0;

    return tp.tv_sec * 1e9 + tp.tv_nsec;
}

double
hpTimerUnit(void)
{
    struct timespec tp;
    long err = clock_getres(MY_CLOCK, &tp);
    if (err)
        return 0.0;
    return ((double)tp.tv_nsec) / 1e9;
}

#endif  /* MS_WINDOWS */