我的意思是:我如何衡量CPU在函数执行上花费的时间,以及运行函数所需的挂钟时间?(我对Linux/Windows以及x86和x86_64都感兴趣).看看我想做什么(我在这里使用C++,但我更喜欢C解决方案):

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";

Another important question: is this type of time measuring architecture independent or not?

推荐答案

这是一个既能在Windows上工作,又能在C和C++上工作的复制粘贴解决方案.

正如 comments 中提到的,有一个boost库可以实现这一点.但如果你不能使用boost,这应该是可行的:

//  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;
    }
}

//  Posix/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

有很多方法可以实现这些时钟.但下面是上面的代码片段使用的内容:

对于Windows:

对于Linux:


下面是一个小演示:

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

int main(){

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

    //  Perform some computation.
    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
    cout << endl;
    cout << "Sum = " << sum << endl;

}

输出(12个线程):

Wall Time = 15.7586
CPU Time  = 178.719

Sum = 2.20259e+011

C++相关问答推荐

为什么这个gcc迂腐的人忽视了扩展?

MISIX标准和信号量的语义

librsvg rsvg_handle_get_dimensions获取像素大小与浏览器中的渲染大小没有不同

想了解 struct 指针和空指针转换

C:gcc返回多个错误定义,但msvc—不""'

在C中使用强制转换将uint16_t转换为uint8_t [2]是否有效?

使用单个字节内的位字段

C:二进制搜索和二进制插入

如何使用低级C++写出数值

为什么I2C会发送错误的数据?

初始变量重置后,char[]的赋值将消失

如何使解释器存储变量

S将C语言宏定义为自身的目的是什么?(在glibc标题中看到)

在创建动态泛型数组时,通过realloc对故障进行分段

收到不兼容的指针类型警告,因为函数的返回不是空*,而是 struct 指针

从不兼容的指针类型返回&&警告,但我看不出原因

GETS()在C++中重复它前面的行

Ubuntu编译:C中的文件格式无法识别错误

C/C++编译器可以在编译过程中通过按引用传递来优化按值传递吗?

malloc:损坏的顶部大小无法找出问题