Home  >  Article  >  Backend Development  >  How to Measure Processor and Real-Time Execution Duration on Linux and Windows?

How to Measure Processor and Real-Time Execution Duration on Linux and Windows?

Barbara Streisand
Barbara StreisandOriginal
2024-11-13 02:45:021002browse

How to Measure Processor and Real-Time Execution Duration on Linux and Windows?

How to Measure Processor and Real-Time Execution Duration on Linux and Windows

Determining a program's CPU and real-time execution duration is crucial for performance optimization. Here's how to achieve this on Linux and Windows, supporting both x86 and x86_64 architectures.

Function Execution and Wall Clock Time Measurement

To measure the CPU time spent by your function and the wall clock time it takes to run, use the following code:

int startcputime, endcputime, wcts, wcte;

startcputime = cputime();
function(args);
endcputime = cputime();

std::cout << "it took " << endcputime - startcputime << " s of CPU to execute this\n";

wcts = wallclocktime();
function(args);
wcte = wallclocktime();

std::cout << "it took " << wcte - wcts << " s of real time to execute this\n";

Platform-Independent Architecture

The time measuring approach presented is architecture-independent, meaning it can be implemented and provide consistent results across different processor architectures.

Implementation

Here's a versatile solution that works for Windows and Linux in both C and C :

//  Windows
#ifdef _WIN32
#include <Windows.h>
double get_wall_time(){
    LARGE_INTEGER time, freq;
    if (!QueryPerformanceFrequency(&freq)){
        //  Handle error
        return 0;
    }
    if (!QueryPerformanceCounter(&time)){
        //  Handle error
        return 0;
    }
    return (double)time.QuadPart / freq.QuadPart;
}
double get_cpu_time(){
    FILETIME a, b, c, d;
    if (GetProcessTimes(GetCurrentProcess(), &a, &b, &c, &d) != 0){
        //  Returns total user time.
        //  Can be tweaked to include kernel times as well.
        return
            (double)(d.dwLowDateTime |
            ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
    }else{
        //  Handle error
        return 0;
    }
}

//  Linux
#else
#include <time.h>
#include <sys/time.h>
double get_wall_time(){
    struct timeval time;
    if (gettimeofday(&time, NULL)){
        //  Handle error
        return 0;
    }
    return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
double get_cpu_time(){
    return (double)clock() / CLOCKS_PER_SEC;
}
#endif

Specific Platform Implementation:

  • Windows:

    • Wall Time: Performance Counters
    • CPU Time: GetProcessTimes()
  • Linux:

    • Wall Time: gettimeofday()
    • CPU Time: clock()

Demonstration

Here's a simple example showcasing the implementation:

#include <math.h>
#include <iostream>
using namespace std;

int main(){

    //  Start Timers
    double wall0 = get_wall_time();
    double cpu0 = get_cpu_time();

    //  Computational task (e.g., numerical summation).
    double sum = 0;
#pragma omp parallel for reduction(+ : sum)
    for (long long i = 1; i < 10000000000; i++){
        sum += log((double)i);
    }

    //  Stop timers
    double wall1 = get_wall_time();
    double cpu1 = get_cpu_time();

    cout << "Wall Time = " << wall1 - wall0 << endl;
    cout << "CPU Time = " << cpu1 - cpu0 << endl;

    //  Prevent code elimination (optimization).
    cout << endl;
    cout << "Sum = " << sum << endl;
}

This code measures the wall clock and CPU time taken by a hypothetical numerical summation.

The above is the detailed content of How to Measure Processor and Real-Time Execution Duration on Linux and Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn