Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: nikitouzz on November 01, 2013, 08:31:52 pm

Title: Opitmizer ?
Post by: nikitouzz on November 01, 2013, 08:31:52 pm
Code: [Select]
#include <iostream>
#include <math.h>
using namespace std;
int main(void)
{int i,k,j,s,nb,rmin,rmax;
float L,t,pi,angler,x,y;
bool finRadius;
nb = 10;
rmin = 5;
rmax = 14;
pi = 4.0*atan(1.0);
angler = 2.0*pi/nb;
i=j=0;
for(k=0;k<rmax*4*rmax-1;k++)
        {x=j-rmax;
        y=i-rmax;
        L=sqrt(x*x+y*y);
        t=acos(x/L);
            if (s=0>=y) t=2*pi-t;
            finRadius=0;
            while ((s<nb+1) and (finRadius==0))
                {if (rmin<=L and L<=rmax and (fabs(t-(float)s*angler)<angler/L or fabs(rmin-L)<1))
                    cout <<(finRadius=1)-1<<"0"; else s++;}
            if (s==nb+1) cout << "..";
            if (i++==rmax*2) {i=0;j++;cout <<endl;}
        }
return 0;}

I have this code in c++ someone can help me to optimise this ? :)
Title: Re: Opitmizer ?
Post by: Adriweb on November 01, 2013, 08:38:49 pm
Is the truly horrible indentation an actual wanted thing (like, in order to 'obfuscate' a little) ? :D

A bit better for the reader :

Code: [Select]
int main(void)
{
int i, k, j, s, nb, rmin, rmax;
float L, t, pi, angler, x, y;
bool finRadius;
nb = 10;
rmin = 5;
rmax = 14;
pi = 4.0*atan(1.0);
angler = 2.0*pi / nb;
i = j = 0;
for (k = 0; k < rmax * 4 * rmax - 1; k++)
{
x = j - rmax;
y = i - rmax;
L = sqrt(x*x + y*y);
t = acos(x / L);
if (s = 0 >= y) t = 2 * pi - t;
finRadius = 0;
while ((s < nb + 1) and (finRadius == 0))
{
if (rmin <= L and L <= rmax and (fabs(t - (float)s*angler) < angler / L or fabs(rmin - L) < 1)) {
cout << (finRadius = 1) - 1 << "0";
} else {
s++;
}
}
if (s == nb + 1) cout << "..";
if (i++ == rmax * 2) { i = 0; j++; cout << endl; }
}
return 0;
}

And as said on TI-Planet, please use && and || instrad of "and" and "or"
Title: Re: Opitmizer ?
Post by: shmibs on November 02, 2013, 07:01:23 pm
firstly: C++ is not Axe
secondly: C++ is not Java
thirdly: C++ is not C

optimised for readability :P

Code: [Select]
#include <iostream>
#include <cmath>

#define PI 4.0 * atan(1.0)
#define NB 10
#define RMIN 5
#define RMAX 14
#define ANGLER 2.0 * PI / NB

using namespace std;

int main() {

int i, k, j, s;
float L, t, x, y;
bool rfin;

i = j = 0;

for (k = 0; k < RMAX * RMAX * 4 - 1; k++) {

x = j - RMAX;
y = i - RMAX;
L = sqrt(x*x + y*y);
t = acos(x / L);

if (s = 0 >= y)
t = 2 * PI - t;

rfin = false;

while ( (s < NB + 1) && (rfin == false) ) {

if (
RMIN <= L &&
L <= RMAX &&
( fabs(t - s * ANGLER) < ANGLER / L || fabs(RMIN - L) < 1 )
)
{
rfin = true;
cout << "00";
} else {
s++;
}

}

if (rfin == false)
cout << "..";

if (i++ == RMAX * 2) {
i = 0;
j++;
cout << endl;
}

}

return 0;

}

this will also be a bit faster already as well. using constants rather than defining the values as variables means that all the values derived from those constants plus other values will simply be interpreted as constants as well (whatever compiler you're using might be smart enough to do this on it's own, but it's still better not to rely on it). also, your
Code: [Select]
cout <<(finRadius=1)-1<<"0";is not only less readable than
Code: [Select]
cout << "00";
rfin = true;
but it's slower as well. in your code, you set a variable, decrement the resulting value, convert that value to a string, stream it out, and then define and stream out a string after that. in mine, i stream out a string and then set a variable (again, since these things are always constant, it's possible that your compiler might clean it up for you, but there's no reason to depend on that).

i'd do more, but am really excited to get back to a project i'm working on =D. just be sure that, if you ever send code to someone else, it's in a readable format. if they can't tell what you're doing and have to spend 30 minutes trying to re-format your code before even starting to look for problems or improvements then you won't find many people willing to help.