🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #2024

Re: cout vs printf speed (was: Should I learn C or C++ first?)

#2024
From: Per Bothner
Date: Wed, 30 Aug 1995 01:02
35 lines
2006 bytes
In article <41vss8$7lq@gap.cco.caltech.edu>,
 <lemahieu@paradise.caltech.edu> wrote:
>I was under the impression that cout and cin for I/O was actually _faster_
>than using printf.

No way.

>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.
>What is the logical argument for printf being faster?

First, note that using ostream uses an extra level of encapsulation,
because output to an ostream has invokes streambuf operations;
the latter are the ones that actually do the I/O.

Output to a streambuf is comparable to output to a FILE, but is
likely to be a little slower.  This is partly because streambuf
I/O is defiend in terms of virtual functions, which is more
flexible, but slightly less efficient.

Also, note that *every* operator<< must call ostream::opfx() and
ostream::osfx(), which adds more overhead.

In addition, writing to cout is likely to even slower if your
implementation follows the ANSI standard, because ANSI requires
that you can mix output to stdout and cout, and still expect the
output in the order written - i.e. cout cannot buffer any output
(unless stdout can find the data in cout's buffer).

-- 
	--Per Bothner
Cygnus Support     bothner@cygnus.com
Technical Details
Message-ID: <420dao$6sd@rtl.cygnus.com>
Path: arc1-ca.pugleaf.net!cygnus.com!not-for-mail
References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41tjep$2hn@deyke3.fc.hp.com> <41v8k9$jq8@tpd.dsccc.com> <41vss8$7lq@gap.cco.caltech.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!cygnus.com!not-for-mail
From: bothner@cygnus.com (Per Bothner)
Newsgroups: rec.games.programmer,comp.lang.c++
Subject: Re: cout vs printf speed (was: Should I learn C or C++ first?)
Date: 29 Aug 1995 18:02:16 -0700
Organization: Cygnus Support, Mountain View, CA
Message-ID: <420dao$6sd@rtl.cygnus.com>
References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41tjep$2hn@deyke3.fc.hp.com> <41v8k9$jq8@tpd.dsccc.com> <41vss8$7lq@gap.cco.caltech.edu>
NNTP-Posting-Host: rtl.cygnus.com