2

I'm new to C/C++ and MEX functions. I created the following function and I would like to optimise it (performance-wise):

#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));
        }
    }
}

}

I heard profiling in MEX is very difficult, so I created a Win32 console project using Microsoft Visual C++ 2010 Express which calls the function 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;
}

But then, what's next? Which (free) profiler is recommended for the optimization of the function GW? I tried using "Very Sleepy v0.82", but couldn't get any results. There's probably something obvious I'm doing wrong...

Thanks in advance!

Praetorian
  • 106,671
  • 19
  • 240
  • 328
SgtPepper88
  • 93
  • 1
  • 7
  • http://stackoverflow.com/a/5672554/103167 – Ben Voigt Feb 14 '13 at 16:39
  • There's no loops or array operations in this function, and it only calls straightforward standard math functions. Are you sure that its speed is actually a problem? – Danica Feb 14 '13 at 19:43

0 Answers0