Author Topic: Opitmizer ?  (Read 3517 times)

0 Members and 1 Guest are viewing this topic.

Offline nikitouzz

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 215
  • Rating: +22/-1
    • View Profile
Opitmizer ?
« 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 ? :)
mes records personels :

2x2x2 : 2.18 secondes / 2x2x2 une main : 21.15 secondes / 2x2x2 yeux bandés : 47.59
3x3x3 : 5.97 secondes / 3x3x3 une main : 49.86 secondes
4x4x4 : 1.49 minutes / 4x4x4 une main : 6.50 minutes
5x5x5 : 4.10 minutes / 5x5x5 une main : 18.02 minutes
6x6x6 : 8.10 minutes
7x7x7 : 16.03 minutes
9x9x9 : 58.26 minutes

megaminx : 5.59 minutes / pyraminx : 7.91 secondes / square-one : 1.07 minutes

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Opitmizer ?
« Reply #1 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"
« Last Edit: November 01, 2013, 08:40:26 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline shmibs

  • しらす丼
  • Administrator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2132
  • Rating: +281/-3
  • try to be ok, ok?
    • View Profile
    • shmibbles.me
Re: Opitmizer ?
« Reply #2 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.
« Last Edit: November 02, 2013, 07:03:58 pm by shmibs »