2

我是 C/C++ 和 MEX 函数的新手。我创建了以下函数,我想优化它(性能方面):

#include "math.h"

double GW(double i1, double i2, double h0, double C1, double C2, double C3, double m, double b, double zc, double hg)
{
double y0, g;

g = 9.81;

if (i1 >= i2) {

    y0 = zc + h0;

    if (i1 - zc < 0) {
        return 9999999;
    } else if ((i1 - y0 < 0) && (y0 > zc)) {
        return 8888888;
    } else {
        double ft, Qt, fg, hp, theta, phi, psi, Qg;

        if ((i2-zc)/(i1-zc) > 0.91+0.09*m) {
            ft = (1-(i2-zc)/(i1-zc))/((1-m)*0.3);
        } else if ((i2-zc)/(i1-zc) >= m) {
            ft = pow((1-(i2-zc)/(i1-zc))/(1-m),0.5);
        } else {
            ft = 1;
        }
        Qt = 0.5*C1*b*sqrt(g)*sqrt((i1-zc)*(i1-zc)*(i1-zc))*ft;

        if ((i2-y0)/(i1-y0) > 0.91+0.09*m) {
            fg = (1-(i2-y0)/(i1-y0))/((1-m)*0.3);
        } else if ((i2-y0)/(i1-y0) >= m) {
            fg = pow((1-(i2-y0)/(i1-y0))/(1-m),0.5);
        } else {
            fg = 1;
        }
        hp = (i1-y0)/(y0 - zc);
        theta = 57.3*asin((y0-zc)/hg);
        phi = (theta-30)/60;
        if (theta >= 30) {
            psi = 0.711*(1-phi)+0.58*phi*(1+0.13*hp);
        } else {
            psi = 0.711;
        }
        Qg = C2*b*pow(g,0.5)*psi*sqrt((i1-y0)*(i1-y0)*(i1-y0))*fg;

        if (Qt < Qg) {
            return Qt;
        } else {
            return Qg;
        }
    }
} else if (i1 < i2) {
    y0 = zc + h0;
    if (i2 - zc < 0) {
        return 77777;
    } else if ((i2 - y0 < 0) && (y0 > zc)) {
        return 666666;
    } else {
        double theta, psi;
        theta = 57.3*asin((y0-zc)/hg);
        if (theta < 22) {
            psi = 0.611*theta/22+0.466*(1-theta/22);
        } else {
            psi = 0.611;
        }
        if ((i2-y0)/(i1-y0) < m) {
            return -C3*b*pow(g,0.5)*psi*sqrt((i2-y0)*(i2-y0)*(i2-y0));
        } else if ((i2-y0)/(i1-y0) > m) {
            double fg;
            if ((i1-y0)/(i2-y0) > 0.91 + 0.09*m) {
                fg = (1-(i1-y0)/(i2-y0))/((1-m)*0.3);
            } else if ((i1-y0)/(i2-y0) > m) {
                fg = pow((1-(i1-y0)/(i2-y0))/(1-m),0.5);
            } else {
                fg = 1;
            }
            return -fg*C3*b*pow(g,0.5)*psi*sqrt((i2-y0)*(i2-y0)*(i2-y0));
        }
    }
}

}

我听说在 MEX 中进行分析非常困难,所以我使用 Microsoft Visual C++ 2010 Express 创建了一个 Win32 控制台项目,它调用了函数 GW:

#include <stdio.h>
#include <iostream>

/* Number of tests. */
 #define N 10000

 #define input1 35
 #define input2 34
 #define input3 2

 #define input4 1
 #define input5 1
 #define input6 1

 #define input7 1
 #define input8 4
 #define input9 30
 #define input10 8

int main() {
    //std::cout << "Geactiveerd!";
int i;
//char holdExecutionWindowOpen;

//std::cin >> holdExecutionWindowOpen;

 for (i = 0; i < N; ++i)
 {
     GW(input1, input2, input3, input4, input5, input6, input7, input8, input9, input10);
 }
return 0;
}

但是,接下来呢?推荐使用哪个(免费)分析器来优化功能 GW?我尝试使用“非常困 v0.82”,但没有得到任何结果。可能有一些明显的我做错了......

提前致谢!

4

0 回答 0