delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/28/01:23:50

Date: Tue, 28 Jan 1997 01:19:08 -0500 (EST)
From: Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu>
To: jz AT bcsupernet DOT com
cc: djgpp AT delorie DOT com
Subject: re:printf("%.6f")
In-Reply-To: <5cgfka$pp0@clark.zippo.com>
Message-ID: <Pine.GSO.3.95.970128011210.27048A-100000@hops.cs.jhu.edu>
MIME-Version: 1.0

On 26 Jan 1997 jz AT bcsupernet DOT com wrote:

> >On 25 Jan 1997 20:51:52 GMT, jz AT bcsupernet DOT com wrote:
> >
> >>Can anyone tell me why my printf("%.6f") is running so incredibly 
> slowly?
> >>I'm running gcc on a 386 with no copro if that's a clue.  The same 
> line 
> >>compiled under Borland C is MUCH faster.  this is more than a pmode 
> problem 
> 
> >Is this the only line in your program?  If so, then the program 
> compiled 
> >under DJGPP will always run slower.  This is because DJGPP spends a 
> lot of 
> >time in startup code compared to a single library function like 
> printf().
> 
> No, it isn't the only line in my program, but it is the offending code 
> in 
> terms of speed.  I realize it is an incomplete statement.  The speed 
> problem 
> is converning the %.6f conversion in formatted output.  I know this 
> because I 
> tried sprintf, printf and fprintf all with the same results.  I also 
> removed 
> the %.6f conversions and the problem disappeared.  The surrounding code 
> is not 
> rellevant to the problem.  For example 
> 	main(){
> 	  float a,b,c;

This may sound a little counterintuitive, but sometimes you can get better
results by using doubles rather than floats.  (Although in DJGPP they're
usually about the same amount of time.  My (relatively slow) 5x86 runs
this program in around 3 seconds (with background processes).

> 	  int l;
> 	
> 	  a=3.24;
> 	  b = 0.00423;
> 	  c = 3464.99887;
> 	  for(l=1;l <1000;l++){
>  	    printf("%.6f  %.6f  %.6f\n", a,b,c);
> 	    a+=3.55;
> 	    b*=1.4;
> 	    c-= .00024;
> 	  }
>         }
> 
> Each iteration of the loop is taking about a half a second.  the entire 
> program takes just under 6 minutes to run which is just silly. The same 
> program with the %.6f's taken out takes 10 seconds.  With the entire 
> printf removed it takes four seconds.  So, obviously the %.6f 
> conversion is very very slow.  the question I have is why?  And do I 
> have to code my own routine to convert from float to text or does 
> anyone know of one I can just go get (I'm lazy) :)   thankyou.
> 
> 
> >Look at sprintf().  It does everything printf() does, except the 
> output is 
> sent
> >to a character array (string) which is automatically null-terminated.
> 
> fprintf, sprintf and printf all take similar lengths of time on %.6f
> 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019