473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is std::cout slower than std::printf

Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.
--
Imanpreet Singh Arora
Jul 22 '05 #1
12 3521
On 22 Jun 2004 00:24:10 -0700, Minti <mi************@yahoo.com> wrote:
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.


The printf call would require a run time examination of the format
string in order to determine the types of the arguments.

All (or most, or some) of those operator<< calls could be in-lined by
the compiler. Certainly for built-in types (which is all that can be
output by printf, as far as I know) the method call overhead should be
in-lined away (of course there are issues with references and
inheritance and the joys of dynamic dispatch).

I would expect a good optimising compiler to produce faster code in the
cout case, while a less optimising compiler might produce faster code in
the printf case (by not optimising away that function call overhead,
assuming that the printf function was implemented reasonably...)

Of course a fancy compiler could notice a constant format string and
know about printf and do something to make debugging hell on earth...

Note: I am not a C++ compiler author, or an optimising compiler of any
flavour author, in fact by the time I've written this someone who is
has probably answered anyway and disagreed with me.

--
Sam Holden
Jul 22 '05 #2

"Minti" <mi************@yahoo.com> wrote in message news:e8**************************@posting.google.c om...
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.
--
Imanpreet Singh Arora


See relevant discussion at
http://groups.google.com/groups?thre...nix2.panix.com
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #3

"Minti" <mi************@yahoo.com> wrote in message
news:e8**************************@posting.google.c om...
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.
--
Imanpreet Singh Arora


My first comment is who cares? std::cout is superior in functionality and
for most people that is more important. My second comment is that if you
really do care then write some code and time them. That is the only way to
answer your question. My expectation would that std::cout is indeed slower,
but that wouldn't stop me using it.

john
Jul 22 '05 #4
Minti wrote:
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.


printf needs to parse the format string at runtime. Further, I doubt
that in a typical implementation _all_ the possible conversions are
implemented withn the printf function itself. So I'd expect printf to
also do the 20 function calls internally.

Jul 22 '05 #5
John Harrison wrote:
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20
calls to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.
--
Imanpreet Singh Arora
My first comment is who cares? std::cout is superior in functionality
and for most people that is more important.


In which way is it superior in functionality?
My second comment is that if you really do care then write some code
and time them. That is the only way to answer your question. My
expectation would that std::cout is indeed slower, but that wouldn't
stop me using it.


Are you just guessing that or is there a reason why you think so?

Jul 22 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cb*************@news.t-online.com...
John Harrison wrote:
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20
calls to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.
--
Imanpreet Singh Arora
My first comment is who cares? std::cout is superior in functionality
and for most people that is more important.


In which way is it superior in functionality?


I should have said the iostream library as a whole is superior to the stdio
library. I was thinking of the way that you can

1) Output user defined types with iostream
2) Define new types of stream
3) Write code that is independent of the type of stream you want to input
from or output to

None of those is possible with stdio.
My second comment is that if you really do care then write some code
and time them. That is the only way to answer your question. My
expectation would that std::cout is indeed slower, but that wouldn't
stop me using it.


Are you just guessing that or is there a reason why you think so?


I've never tested it myself, but I seen a few times when posters to this
group have tested it. My recollection is that iostream was slower for those
posters, but of course this is entirely platform dependent. As I said, for
me in the sort of work I do, its never really been an issue, I realise that
is not the case for everyone.

john
Jul 22 '05 #7
On 22 Jun 2004 00:24:10 -0700, mi************@yahoo.com (Minti) wrote:
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.


In theory, cout could be faster than printf. In practice, it isn't.
Typically operator<< methods follow this pattern:

ostream::sentry cerberos(os); //flushes tied streams
if (cerberos) //checks for good state
{
try
{
//do output, using facet and streambuf
}
catch(...)
{
//set badbit and possibly rethrow;
}
//if there's an error,
//set failbit/eofbit and possibly throw.
}
return os;
//here stream is flushed by ~sentry if unitbuf is set.

As you can see, that's quite a lot to do and is unlikely to be
inlined. If you split the printf into 20 printf's, performance might
be a bit closer.

I think the above can be implemented a lot better, optimizing the
common case (no tied streams, not need to flush). If that is done, and
the formatting facets similarly optimized, then ostreams can be
faster. Apparently cxxrt, Dietmar Kuehl's standard C++ library
implementation, exceeds cstdio performance in many cases.

You should also look at the C++ performance technical report:
http://www.open-std.org/jtc1/sc22/wg.../PDTR18015.pdf
(particularly chapter 3)

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8
mi************@yahoo.com (Minti) wrote in message news:<e8**************************@posting.google. com>...
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.


It is possible to collect cpu time of execution by the use of ANSI C
clock() before and after some operation in order to extract the
duration as a difference.
When using clock() don't forget to divide this difference by the macro
CLOCKS_PER_SEC .

If you have access to a Linux/Unix OS the times() system call can be
used too to get real and cpu time.
The main advantage of the latter is that also child time can be
collected.

Ciao,

Fabio De Francesco.
Jul 22 '05 #9
sh*****@flexal.cs.usyd.edu.au (Sam Holden) wrote:
I would expect a good optimising compiler to produce faster code in the
cout case, while a less optimising compiler might produce faster code in
the printf case (by not optimising away that function call overhead,
assuming that the printf function was implemented reasonably...)
Actually, that was my assumption way back before I started to implement my
own standard C++ library. As it turns out, it is pretty tough to arrive at
the same speed as the stdio-family with standard IOStreams: you need to go
through a whole bunch of tricks to avoid overheads. This concerns things
like caching result of the ctype and numpunct facets, short-circuiting
sentry construction, folding flags and conditions, etc. Effectively, this
results in roughly the same performance as stdio (if anybody has results
about current implementations, I would be quite interested, especially if
the IOStreams library shows far better performance than stdio). I'm not
sure whether such enhancements are applied to other library implementations
(maybe the other library implementers can say something on this issue...).
Note: I am not a C++ compiler author, or an optimising compiler of any
flavour author, in fact by the time I've written this someone who is
has probably answered anyway and disagreed with me.


I don't think that the optimising compiler can do much here although there
will be a *huge* performance difference between optimized and unoptimized
IOStreams. With IOStreams and locales being templates often located in
header files, optimized compilation will take quite a while at least with
the compilers I tested it (mostly gcc and Sun CC). However, many of the
needed performance gains are in the library implementation.

BTW, you can download my implementation form
<http://www.dietmar-kuehl.de/cxxrt/> if you want to look at it.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #10
sh*****@flexal.cs.usyd.edu.au (Sam Holden) wrote:
I would expect a good optimising compiler to produce faster code in the
cout case, while a less optimising compiler might produce faster code in
the printf case (by not optimising away that function call overhead,
assuming that the printf function was implemented reasonably...)
Actually, that was my assumption way back before I started to implement my
own standard C++ library. As it turns out, it is pretty tough to arrive at
the same speed as the stdio-family with standard IOStreams: you need to go
through a whole bunch of tricks to avoid overheads. This concerns things
like caching result of the ctype and numpunct facets, short-circuiting
sentry construction, folding flags and conditions, etc. Effectively, this
results in roughly the same performance as stdio (if anybody has results
about current implementations, I would be quite interested, especially if
the IOStreams library shows far better performance than stdio). I'm not
sure whether such enhancements are applied to other library implementations
(maybe the other library implementers can say something on this issue...).
Note: I am not a C++ compiler author, or an optimising compiler of any
flavour author, in fact by the time I've written this someone who is
has probably answered anyway and disagreed with me.


I don't think that the optimising compiler can do much here although there
will be a *huge* performance difference between optimized and unoptimized
IOStreams. With IOStreams and locales being templates often located in
header files, optimized compilation will take quite a while at least with
the compilers I tested it (mostly gcc and Sun CC). However, many of the
needed performance gains are in the library implementation.

BTW, you can download my implementation form
<http://www.dietmar-kuehl.de/cxxrt/> if you want to look at it.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #11
mi************@yahoo.com (Minti) wrote in message news:<e8**************************@posting.google. com>...
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }


They'll typically both run as fast as the I/O subsystem supports,
though iostreams will typically require more CPU usage to do it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #12
On 22 Jun 2004 00:24:10 -0700, mi************@yahoo.com (Minti) wrote
in comp.lang.c++:
Is std::cout slower than printf
When we call printf e.g. in

printf(20 format conversion specifications, 20 arguments);

Is it faster than the

std::cout << { 20 redirections to the output stream }
It seems {quite obvious}[1] to me that this @should@ infact be true
this is because in a sense the std::cout method would require 20 calls
to the overloaded operator << on ostream.

[1] I can clearly see you taking your guns out.


Yes, in fact the ISO C++ standard requires that std::cout be at least
27.2% slower than printf() in all cases.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
2
by: Tony Murphy | last post by:
I've got an application that sends emails (not spam!). The application reads a template file (html/text) into a string, the string is processed and placeholders filled in as appropiate. I call a...
12
by: Michael Mellor | last post by:
I see so many posts on this newsgroup where people use std::endl but I can't see why or where they are getting it from. std::endl outputs a new line and flushes the stream's buffer. It is even 15.7...
6
by: Christopher Benson-Manica | last post by:
Is there anything that one can do to a std::ostringstream that would make its destructor explode? I'm basically doing { std::ostringstream ss, msg; // do BUNCHES of stuff with ss and msg ...
3
by: Richard Cavell | last post by:
GCC 3.3, XCode: #include <iostream> #include <stdint.h> int main (int argc, char * const argv) { uint64_t a=10123123123LL; printf("%d\n", a);
4
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
1
by: Adam Teasdale Hartshorne | last post by:
Using the code below I get a very stange result. I assume that I must be doing something wrong, but I can't figure out what it is. getTriangleEdges(t,t1e) ; for (int i = 0 ; i <...
10
by: abubakarm | last post by:
Hi, How can I write a number 123456 formatted as 123,456 using printf? Regards, ...ab
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.