Article View: rec.games.programmer
Article #2025Re: cout vs printf speed (was: Should I learn C or C++ first?)
From: Tom Gooding
Date: Wed, 30 Aug 1995 05:16
Date: Wed, 30 Aug 1995 05:16
33 lines
2217 bytes
2217 bytes
In article <41vst0$9nb@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. 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? > > If anyone has any numbers on stream I/O vs printf, I'd be interested in > hearing them. Although I haven't timed cout vs printf. I have timed similarly implemented functions based on printf and cout. If your string has a LOT of variables, then printf *may* be faster, because the overhead in calling the '<<' operators for each variable. However, most of the time, the cout-type implementation was generally faster. In my case, the cout-type routines were about 30% faster than the printf-type. These strings usually had 2 variables and strlen was usually about 40 characters. Of course, longer contiguous strings with fewer variables performed best. My printf-type routine didn't have too many variable types to check for, I would guess that if it had many variable types to sort through, cout's performance would be superior (since the function call is decided at compile time). -- Tom Gooding tgooding@iastate.edu Computer Engineering 4
Technical Details
Message-ID:
<tgooding-3008950016040001@helser196.res.iastate.edu>Path:
arc1-ca.pugleaf.net!helser196.res.iastate.edu!userReferences:
<DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <41kici$p4t@deyke3.fc.hp.com> <41ngln$qau@druid.borland.com> <41tjep$2hn@deyke3.fc.hp.com> <41v8k9$jq8@tpd.dsccc.com> <41vst0$9nb@gap.cco.caltech.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!helser196.res.iastate.edu!user From: tgooding@iastate.edu (Tom Gooding) Newsgroups: rec.games.programmer,comp.lang.c++ Subject: Re: cout vs printf speed (was: Should I learn C or C++ first?) Date: Wed, 30 Aug 1995 00:16:04 -0500 Organization: Iowa State University, Ames, Iowa Message-ID: <tgooding-3008950016040001@helser196.res.iastate.edu> References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <41kici$p4t@deyke3.fc.hp.com> <41ngln$qau@druid.borland.com> <41tjep$2hn@deyke3.fc.hp.com> <41v8k9$jq8@tpd.dsccc.com> <41vst0$9nb@gap.cco.caltech.edu> NNTP-Posting-Host: helser196.res.iastate.edu