执行我的简单功能需要多少CPU周期?

时间:2015-03-07 17:18:27

标签: c++ c optimization profiling

出于教育目的,我想知道在优化(在不同级别)和编译后执行函数需要多少CPU周期。有没有办法分析代码或可执行文件以获得可重现的答案?我在64位Windows 7 Pro上使用Eclipse Luna和MinGW。

#include <math.h>
#include "main.h"

#define EPS 1e-15 // EPS a small number ~ machine precision
#define R2D 57.295779513082320876798154814105   //multiply radian with R2D to get degrees
#define D2R 0.01745329251994329576923690768489  //multiply degrees with D2R to get radians
#define TWO_PI 6.283185307179586476925286766559 //2*Pi


double _stdcall CourseInitial (double *lat1, double *lon1, double *lat2, double *lon2)
{
    double radLat1     = D2R *  *lat1;
    double radLat2     = D2R *  *lat2;
    double radDeltaLon = D2R * (*lon2 - *lon1);
    double tc = 0;

    if (cos(radLat1) < EPS) {  // EPS a small number ~ machine precision
        if (radLat1 > 0) {
          tc = 180;            // Starting at N pole
        } else {
          tc = 0;              // Starting at S pole
        }
    } else {
      // Calculate true course [-180, 180)
      tc = R2D * atan2(sin(radDeltaLon),
                       cos(radLat1) * tan(radLat2) - sin(radLat1) * cos(radDeltaLon)
                      );
    }

    if (fabs(tc) < EPS) {
        tc = 0;  //Prevents fmod(tc, 360) from returning 360 due to rounding error
    } else {
        tc += 360; //tc [180, 540)
    }
    return fmod(tc, 360); // returns tc [0, 360)
}

int main(void)
{
    double lat1 = 67
    double lon1 = 15;
    double lat2 = 71;
    double lon2 = 24;
    double tc = 0;
    tc = CourseInitial(&lat1, &lon1, &lat2, &lon2);
    printf("The course from point 1 to 2 is: %.1f\n", tc);

    return 0;
}

1 个答案:

答案 0 :(得分:2)

我这样做了:

#include <math.h>
#include <cstdlib>
#include <cstdio>

#define EPS 1e-15 // EPS a small number ~ machine precision
#define R2D 57.295779513082320876798154814105   //multiply radian with R2D to get degrees
#define D2R 0.01745329251994329576923690768489  //multiply degrees with D2R to get radians
#define TWO_PI 6.283185307179586476925286766559 //2*Pi


double CourseInitial (double *lat1, double *lon1, double *lat2, double *lon2)
{
    double radLat1     = D2R *  *lat1;
    double radLat2     = D2R *  *lat2;
    double radDeltaLon = D2R * (*lon2 - *lon1);
    double tc = 0;

    if (cos(radLat1) < EPS) {  // EPS a small number ~ machine precision
        if (radLat1 > 0) {
          tc = 180;            // Starting at N pole
        } else {
          tc = 0;              // Starting at S pole
        }
    } else {
      // Calculate true course [-180, 180)
      tc = R2D * atan2(sin(radDeltaLon),
                       cos(radLat1) * tan(radLat2) - sin(radLat1) * cos(radDeltaLon)
                      );
    }

    if (fabs(tc) < EPS) {
        tc = 0;  //Prevents fmod(tc, 360) from returning 360 due to rounding error
    } else {
        tc += 360; //tc [180, 540)
    }
    return fmod(tc, 360); // returns tc [0, 360)
}

struct LatLon
{
    double lat, lon; 
};

struct CoursePoint
{
    LatLon a, b;
};

const int SIZE = 1000000;

CoursePoint cps[SIZE];
double tc[SIZE];

LatLon RandomLatLon()
{
    LatLon l;
    l.lat = rand() % 90;
    l.lon = rand() % 60;
    return l;
}

static __inline__ unsigned long long rdtsc(void)
{
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

int main(void)
{
    for(int i = 0; i < SIZE; i++)
    {
    cps[i].a = RandomLatLon();
    cps[i].b = RandomLatLon();
    }
    unsigned long long t = rdtsc();
    for(int i = 0; i < SIZE; i++)
    {
    tc[i] = CourseInitial(&cps[i].a.lat, &cps[i].a.lon, &cps[i].b.lat, &cps[i].b.lon);
    }
    t = rdtsc() - t;
    printf("Time=%f\n", t/(double)SIZE);
    double tot = 0;
    for(int i = 0; i < SIZE; i++)
    {
    tot += tc[i];
    }
    printf("Sum of courses: %f\n", tot);

    return 0;
}

每次迭代大约需要850-1000个周期。

如果你没有像这样运行长循环,那么虚假的东西会影响实际的性能。编译器优化选项会产生很小的差异,添加-ffast-math会比-O0 vs -O3产生更大的差异。不同的编译器也会有所不同。

g ++ 4.9.2:

-O0    1013 cycles
-O1     879 cycles
-O2     878 cycles
-O3     877 cycles
Add -ffast-math:
-O0     978
-O1     855  (re-run gives 890)
-O2     882
-O3     848  (re-run gives 850)

Clang ++(几周前的3.7):

-O0     998 cycles
-O1     954 cycles
-O2     955 cycles
-O3     957 cycles
Add -ffast-math:
-O0     967
-O1     872
-O2     865
-O3     875

Clang ++截至昨天:

-O0    1001 cycles
-O1     956 cycles
-O2     948 cycles
-O3     949 cycles
Add -ffast-math:
-O0     969
-O1     871
-O2     869
-O3     873

请注意,少于10个时钟周期的差异可能在统计上不显着。我确实报告了一次运行,但我之前尝试了几次以确保大部分时间,我得到相同的(ish)答案。

请注意,不同的处理器会产生完全不同的结果,不同的编译器版本会清楚地显示出一些差异。

编辑:为了好玩,我会将其重写为Pascal并通过我的Pascal编译器运行它以查看它的作用。

使用我的Pascal编译器编译的代码需要881个时钟周期,其中-O2(最高级别可用)。

FreePascal,这是“官方”Linux Pascal编译器没有可用的时钟周期计数器,所以我只做了time ./course,它出现了大约0.44s,我的编译器中的代码是0.37s。