Windows では「 Clock_gettime 」関数をネイティブに使用できないため、どのように実装できますか?

Patricia Arquette
リリース: 2024-10-26 18:14:02
オリジナル
881 人が閲覧しました

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

Windows への Clock_gettime の移植

経過時間の測定と現在時刻の取得に一般的に使用される Clock_gettime システム コールは、ネイティブでは使用できません。 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 システムでの実行時間を測定するようになります。

以上がWindows では「 Clock_gettime 」関数をネイティブに使用できないため、どのように実装できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!