>  기사  >  백엔드 개발  >  기본적으로 사용할 수 없는 `clock_gettime` 함수를 Windows에서 어떻게 구현할 수 있나요?

기본적으로 사용할 수 없는 `clock_gettime` 함수를 Windows에서 어떻게 구현할 수 있나요?

Patricia Arquette
Patricia Arquette원래의
2024-10-26 18:14:02882검색

How can you implement the `clock_gettime` function on Windows, since it isn't available natively?

clock_gettime을 Windows로 포팅

경과 시간을 측정하고 현재 시간을 검색하는 데 일반적으로 사용되는 clock_gettime 시스템 호출은 Windows에서 기본적으로 사용할 수 없습니다. Windows 운영 체제. 그러나 이 함수에 대한 대체 기능을 구현하는 방법이 있습니다.

Windows 구현

다음은 Windows용 clock_gettime 기능을 에뮬레이트하는 샘플 코드 구현입니다.

<code class="c++">#include <Windows.h>

LARGE_INTEGER getFILETIMEoffset() {
    SYSTEMTIME s;
    FILETIME f;
    LARGE_INTEGER t;

    s.wYear = 1970;
    s.wMonth = 1;
    s.wDay = 1;
    s.wHour = 0;
    s.wMinute = 0;
    s.wSecond = 0;
    s.wMilliseconds = 0;
    SystemTimeToFileTime(&s, &f);
    t.QuadPart = f.dwHighDateTime;
    t.QuadPart <<= 32;
    t.QuadPart |= f.dwLowDateTime;
    return (t);
}

int clock_gettime(int X, struct timeval *tv) {
    LARGE_INTEGER t;
    FILETIME f;
    double microseconds;
    static LARGE_INTEGER offset;
    static double frequencyToMicroseconds;
    static int initialized = 0;
    static BOOL usePerformanceCounter = 0;

    if (!initialized) {
        LARGE_INTEGER performanceFrequency;
        initialized = 1;
        usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
        if (usePerformanceCounter) {
            QueryPerformanceCounter(&offset);
            frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
        } else {
            offset = getFILETIMEoffset();
            frequencyToMicroseconds = 10.;
        }
    }
    if (usePerformanceCounter) QueryPerformanceCounter(&t);
    else {
        GetSystemTimeAsFileTime(&f);
        t.QuadPart = f.dwHighDateTime;
        t.QuadPart <<= 32;
        t.QuadPart |= f.dwLowDateTime;
    }

    t.QuadPart -= offset.QuadPart;
    microseconds = (double)t.QuadPart / frequencyToMicroseconds;
    t.QuadPart = microseconds;
    tv->tv_sec = t.QuadPart / 1000000;
    tv->tv_usec = t.QuadPart % 1000000;
    return (0);
}</code>

사용 예

Windows 코드에서 clock_gettime 대체 함수를 활용하려면 원래 QNX 구현에서 제공된 샘플 사용법을 조정할 수 있습니다.

<code class="c++">#define BILLION 1000000000L;

struct timespec start_time;
struct timespec stop_time;

void startMyTestFunc() {
    clock_gettime(CLOCK_REALTIME, &start_time);
    // ... additional code
    cout << "The execution time of func" << calculateExecutionTime();
}

double calculateExecutionTime() {
    clock_gettime(CLOCK_REALTIME, &stop_time);

    double dSeconds = (stop_time.tv_sec - start_time.tv_sec);

    double dNanoSeconds = (double)(stop_time.tv_nsec - start_time.tv_nsec) / BILLION;

    return dSeconds + dNanoSeconds;
}</code>

이 수정된 코드는 이제 Windows 관련 clock_gettime 구현을 사용하여 Windows 시스템의 실행 시간을 측정합니다.

위 내용은 기본적으로 사용할 수 없는 `clock_gettime` 함수를 Windows에서 어떻게 구현할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.