Message-ID: <19990505115507.28110.qmail@www0d.netaddress.usa.net> Date: 5 May 99 04:55:07 PDT From: Oscar Almer To: djgpp AT delorie DOT com Subject: Re: [optimizing a C program] X-Mailer: USANET web-mailer (M3.0.0.105) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id IAA06667 Reply-To: djgpp AT delorie DOT com The optimivation flags you are looking for are -On (where n is a integer between 0 and 9), -expensive-optimizations and -fast-math(i think it is called). Compile with these flags set and you should get around 50% more performance. At least i did. >"Bonifati" wrote: >how can i optimize this little program which calculates N! ? >i don't now what optimization flags to set into RHIDE >/* this program works; i have tested it */ >#define MAXPIECES 36068 /* 36068 are those necessary for 42950! */ >#define N 1000 /* from 0 a 42950 */ >/* a "long int" must be 32 bit (at least) */ >/* "short" must be at least 16 bit, or use int instead */ >unsigned long Fact[MAXPIECES], Product; >unsigned short i, i2, Carry, Index, EndIndex; >main() >{ > /* calculates N FACTORIAL */ > Fact[0]=1; > Index=EndIndex=0; > > for (i=N; i>1; i--) > { > Carry=0; > for (i2=Index; i2<=EndIndex; i2++) > { > Product=Fact[i2]*i + Carry; > Fact[i2]=Product%100000; > Carry=Product/100000; //This could be written with binary //shifting instead. See any tutorial for documentation. > } > /* manage last carry */ > if (Carry) > Fact[++EndIndex]=Carry; > /* adjust Index to optimize */ > while (Fact[Index]==0) > Index++; > } > /* print */ > printf("%ld", Fact[EndIndex]); > for (i2=EndIndex; i2>0;) > printf("%05ld", Fact[--i2]); > putchar('\n'); >} Personally, I woudn't do it like this. I would have used a recursive function to calculate n, and than make a function to call itself with n-1 as parameter, like this: int nfact(int n) { int n1,fact; n1=n-1 if ((n-1) == 0) return 1 fact=n*nfact(n1) //recursive call to same function return fact } But i am not sure this (specified) function does work. it is only something i got from the top of my mind. the tecnique should work, though. Oscar Almer ____________________________________________________________________ Get your own FREE, personal Netscape WebMail account today at http://webmail.netscape.com.