Article View: rec.games.programmer
Article #2028Re: cout vs printf speed (was: Should I learn C or C++ first?)
From: Pete Becker
Date: Thu, 31 Aug 1995 15:53
Date: Thu, 31 Aug 1995 15:53
50 lines
2884 bytes
2884 bytes
In article <423cuc$tab@harbinger.cc.monash.edu.au>, junaid@silas.cc.monash.edu.au says... > >James Kanze US/ESC 60/3/141 #40763 (kanze@lts.sel.alcatel.de) wrote: >: In article <MATT.95Aug29163934@godzilla.EECS.Berkeley.EDU> >: matt@godzilla.EECS.Berkeley.EDU (Matt Austern) writes: >: >: |> In article <41vst0$9nb@gap.cco.caltech.edu> <lemahieu@paradise.caltech.edu> writes: >: >: |> > I was under the impression that cout and cin for I/O was actually _faster_ >: |> > than using printf. I thought this was because printf must parse >: |> > the format string each time to determine how the subsequent parameters >: |> > are to be interpreted and output, whereas this is done at compile time >: |> > for cout. The format string is typically constant but must be reparsed every >: |> > time the printf function is called, which doesn't sound very efficient to me. >: >: |> There's no reason why printf() has to be implemented like that. >: |> Functions in the standard library don't have to be treated as ordinary >: |> functions: an implementation is free to treat them as (almost) part of >: |> the language. There's nothing wrong with a C++ implementation parsing >: |> printf's format string at compile time, doing error checking to >: |> make sure that there aren't any type errors, and putting in the >: |> appropriate low-level calls. >: >: |> I don't know the names of any implementations that actually do this, but >: |> I strongly suspect that there are some. > > Well Gcc 2.6.3 will check the formatting spec, and i'm sure >you could make a small program that converted printf's to lines of >puts(). > >: >: Actually, I doubt it. Normally, the format string in production code >: will not be a string literal, but rather something read from a >: configuration file, dependant on the locale setting. >: >: Or do you really think everyone in the world can read English >: fluently. > > This run-time dynamic behaviour is unique to sprintf() and >friends. There is no way to implement this using cout, becuase the >implicit formatting specification is unknown at compile time. There >for C++ streams are in fact more restrictive than the C lib I/O . About four years ago I wrote an article in The C++ Report showing how to do this.
Technical Details
Message-ID:
<424lti$5cu@druid.borland.com>Path:
arc1-ca.pugleaf.net!druid.borland.com!usenetReferences:
<DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <423cuc$tab@harbinger.cc.monash.edu.au>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!druid.borland.com!usenet From: pete@borland.com (Pete Becker) Newsgroups: rec.games.programmer,comp.lang.c++ Subject: Re: cout vs printf speed (was: Should I learn C or C++ first?) Date: 31 Aug 1995 15:53:22 GMT Organization: Borland International Message-ID: <424lti$5cu@druid.borland.com> References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <423cuc$tab@harbinger.cc.monash.edu.au> NNTP-Posting-Host: sbcandau.borland.com Mime-Version: 1.0 Content-Type: Text/Plain; charset=ISO-8859-1 X-Newsreader: WinVN 0.99.5