Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to measure execution time in clock time and CPU time with Win32 C

2 Answers

0 votes
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

// run with printf

int main(void)
{
    // begine 
    struct timespec begin;
    timespec_get(&begin, TIME_UTC);
    struct _FILETIME beginCPU, creation_time, kernel_time, exit_time;
    GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &beginCPU);

    for (long i = 0; i < 10000; i++) {
        printf("i = %ld\n", i);
    }

    /*
        struct timespec
        {
            time_t tv_sec;  // Seconds - >= 0
            long   tv_nsec; // Nanoseconds - [0, 999999999]
        };
    */
 
    /*
        typedef struct _FILETIME {
            DWORD dwLowDateTime;
            DWORD dwHighDateTime;
        } FILETIME, *PFILETIME, *LPFILETIME;
    */
 
    /*
        GetProcessTimes(
            _In_ HANDLE hProcess,
            _Out_ LPFILETIME lpCreationTime,
            _Out_ LPFILETIME lpExitTime,
            _Out_ LPFILETIME lpKernelTime,
            _Out_ LPFILETIME lpUserTime
        );
    */

    // end 
    struct timespec end;
    timespec_get(&end, TIME_UTC);
    struct _FILETIME endCPU;
    GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &endCPU);
            
    double execute_time = (end.tv_sec - begin.tv_sec) + (end.tv_nsec - begin.tv_nsec) / 1000000000.0;

    ULARGE_INTEGER ulBeginCPU;
    ulBeginCPU.LowPart = beginCPU.dwLowDateTime;
    ulBeginCPU.HighPart = beginCPU.dwHighDateTime;

    ULARGE_INTEGER ulEndCPU;
    ulEndCPU.LowPart = endCPU.dwLowDateTime;
    ulEndCPU.HighPart = endCPU.dwHighDateTime;

    // ULONGLONG QuadPart; // typedef unsigned __int64 ULONGLONG
    double execute_timeCPU = (ulEndCPU.QuadPart - ulBeginCPU.QuadPart) / 10000000.0;

    printf("Execute Time (CLOCK TIME): %lf sec\n", execute_time);
    printf("Execute Time (CPU TIME): %lf sec\n", execute_timeCPU);
}



/*
run:

...

i = 9992
i = 9993
i = 9994
i = 9995
i = 9996
i = 9997
i = 9998
i = 9999
Execute Time (CLOCK TIME): 4.947347 sec
Execute Time (CPU TIME): 0.031250 sec

*/

 



answered Jan 8, 2025 by avibootz
edited Jan 8, 2025 by avibootz
0 votes
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

// run with calculation

int main(void)
{
    // begine 
    struct timespec begin;
    timespec_get(&begin, TIME_UTC);
    struct _FILETIME beginCPU, creation_time, kernel_time, exit_time;
    GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &beginCPU);

    long long n = 12;
    for (long i = 0; i < 100000000; i++) {
        n *= 2;
    }

    /*
        struct timespec
        {
            time_t tv_sec;  // Seconds - >= 0
            long   tv_nsec; // Nanoseconds - [0, 999999999]
        };
    */

    /*
        typedef struct _FILETIME {
            DWORD dwLowDateTime;
            DWORD dwHighDateTime;
        } FILETIME, *PFILETIME, *LPFILETIME;
    */

    /*
        GetProcessTimes(
            _In_ HANDLE hProcess,
            _Out_ LPFILETIME lpCreationTime,
            _Out_ LPFILETIME lpExitTime,
            _Out_ LPFILETIME lpKernelTime,
            _Out_ LPFILETIME lpUserTime
        );
    */

    // end 
    struct timespec end;
    timespec_get(&end, TIME_UTC);
    struct _FILETIME endCPU;
    GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &endCPU);
            
    double execute_time = (end.tv_sec - begin.tv_sec) + (end.tv_nsec - begin.tv_nsec) / 1000000000.0;

    ULARGE_INTEGER ulBeginCPU;
    ulBeginCPU.LowPart = beginCPU.dwLowDateTime;
    ulBeginCPU.HighPart = beginCPU.dwHighDateTime;

    ULARGE_INTEGER ulEndCPU;
    ulEndCPU.LowPart = endCPU.dwLowDateTime;
    ulEndCPU.HighPart = endCPU.dwHighDateTime;

    // ULONGLONG QuadPart; // typedef unsigned __int64 ULONGLONG
    double execute_timeCPU = (ulEndCPU.QuadPart - ulBeginCPU.QuadPart) / 10000000.0;

    printf("Execute Time (CLOCK TIME): %lf sec\n", execute_time);
    printf("Execute Time (CPU TIME): %lf sec\n", execute_timeCPU);
}



/*
run:

Execute Time (CLOCK TIME): 0.148590 sec
Execute Time (CPU TIME): 0.140625 sec

*/

 



answered Jan 8, 2025 by avibootz
edited Jan 8, 2025 by avibootz
...