如何在Linux / Windows上测量CPU时间和挂钟时间?

时间:2013-07-02 17:55:11

标签: c++ c performance time cpu

我的意思是:我如何衡量我的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";

另一个重要问题:这种时间测量架构是否独立?

4 个答案:

答案 0 :(得分:62)

这是一个适用于Windows和Linux以及C和C ++的复制粘贴解决方案。

正如评论中所提到的,有一个提升库可以做到这一点。但如果你不能使用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

答案 1 :(得分:27)

C ++ 11。写得容易多了!

使用"csrftoken=3R8dfLkO6HJatXhlzaifLI8uY96g5479" 作为挂钟,使用csrftoken=2wabMFekQdMdlWtoQ5ugCvzdnz2fPAAu; 作为cpu时钟 http://en.cppreference.com/w/cpp/chrono/system_clock

std::chrono::system_clock

Etvoilà,轻松便携!不需要#ifdef _WIN32或LINUX!

如果您需要更高的精确度,甚至可以使用std::clock http://en.cppreference.com/w/cpp/chrono/high_resolution_clock

答案 2 :(得分:13)

给出一个具体的例子,@ lip的建议如果你能使用boost::timer(用Boost 1.51测试):

#include <boost/timer/timer.hpp>

// this is wallclock AND cpu time
boost::timer::cpu_timer timer;

... run some computation ...

boost::timer::cpu_times elapsed = timer.elapsed();
std::cout << " CPU TIME: " << (elapsed.user + elapsed.system) / 1e9 << " seconds"
          << " WALLCLOCK TIME: " << elapsed.wall / 1e9 << " seconds"
          << std::endl;

答案 3 :(得分:-1)

使用time.h中的clock方法:

clock_t start = clock();
/* Do stuffs */
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;

不幸的是,这个方法在Linux上返回CPU时间,但returns wall-clock time on Windows(感谢评论者提供此信息)。

相关问题