1

Possible Duplicates:
printf vs cout in C++
cin or printf??

I've always wondered about printf and cout.. which one is ultimately faster, and is it the most flexible as well (ie can print a range of variables, and output can be formatted)?

P.S. I know this looks similar to 'printf' vs. 'cout' in C++ ,but i'm not really asking the same thing.

Community
  • 1
  • 1
Greg Treleaven
  • 8,124
  • 7
  • 30
  • 30
  • @Greg, have you read the answers to the question you referenced (other than the accepted answer, which should be ignored)? Some good information there, and it seems to answer your questions. – Michael Petrotta Sep 04 '10 at 20:11
  • 1
    Duplicate of: http://stackoverflow.com/questions/3551319/cin-or-printf – Merlyn Morgan-Graham Sep 04 '10 at 20:11
  • @Michael Petrotta: you obviously didn't read my question fully, i'm not asking one which is better. – Greg Treleaven Sep 04 '10 at 20:12
  • 1
    "which one is ultimately faster": profile your app and determine that text output is the bottleneck before you worry about perf. To facilitate this, you can wrap your printing into modules that are easily re-implemented at a later time. "is it the most flexible as well" I don't know if you can easily customize printf as much as you can cout, though you can ultimately print anything with either. – Merlyn Morgan-Graham Sep 04 '10 at 20:14
  • @Greg, Michael: I agree with Michael. The *answers* cover the information you are looking for, even if the question doesn't. – Merlyn Morgan-Graham Sep 04 '10 at 20:15
  • @Greg: I did read your question, and that's exactly what you're asking. Or are speed and flexibility not good things? – Michael Petrotta Sep 04 '10 at 20:17
  • Both are a *lot* faster than a human can read. – Hans Passant Sep 04 '10 at 20:17
  • @Michael: I was directing at your first comment, which was "Possible duplicate of: http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c" which was what I linked to. It looks like you've deleted it, or editted it out. – Greg Treleaven Sep 04 '10 at 20:21
  • @Greg: that comment is automatically generated by the system when someone votes to close a question as a duplicate (which I did). It's automatically deleted when the question is actually closed. I'm not thrilled with those comments either - it looks like I wrote it, but I didn't. – Michael Petrotta Sep 04 '10 at 20:23
  • "closed as exact duplicate": I don't agree it is an exact duplicate, but I had no idea stackoverflow.com/questions/3551319/cin-or-printf was actually there/ – Greg Treleaven Sep 04 '10 at 20:24
  • @Greg: It's a duplicate of printf vs cout C++. –  Sep 04 '10 at 21:55

2 Answers2

7

Short Answer

Faster : printf

More flexible : cout

Long answer

When compared to the sprintf family, the C++ streams are supposed to be slower (by a factor 6 if I recall an item of Exceptional C++, by Herb Sutter). Still, most of the time, you won't need this speed, but you need to be sure your code won't be bugged.

And it is easy to do something wrong with the printf family of functions, be it putting the wrong number of arguments, the wrong types, or even introduce potential security vulnerability (the %n specifier comes to mind) in your code.

Unless really wanting it (and then, it's called sabotage), it's almost impossible to get it wrong with C++ streams. They handle seamlessly all known types (build-ins, std::strings, etc.), and it's easy to extend it. For example, let's say I have an object "Coordinate3D", and that I want to print out its data:

#include <iostream>

struct Coordinate3D
{
    int x ;
    int y ;
    int z ;
} ;

std::ostream & operator << (std::ostream & p_stream
                          , const Coordinate3D & p_c)
{
    return p_stream << "{ x : " << p_c.x
                   << " , y : " << p_c.y
                   << " , z : " << p_c.z << " }" ;
}

int main(int argc, char * argv[])
{
    Coordinate3D A = {25,42,77} ;
    std::cout << A << std::endl ;
          // will print "{ x : 25 , y : 42 , z : 77 }"
    return 0 ;
}

The problem with the stream is that they are quite difficult to handle correctly when wanting to specify format of some data (padding spaces for numbers, for example), and that sometimes, you really really need to go fast. Then, either fall back to printf, or try some high-speed C++ alternatives (FastFormat comes to mind).

Edit: Note that Thomas' series of tests show interesting results (which I reproduced right now on my computer), that is: cout and printf have similar performances when one avoids using std::endl (which flushes the output in addition to outputing a \n).

Community
  • 1
  • 1
paercebal
  • 81,378
  • 38
  • 130
  • 159
  • Effective C++ was written by Scott Myers. Herb Sutter wrote Exceptional C++. Yes, the names are similar... – E.M. Sep 04 '10 at 20:32
  • @Whisty: Right! I corrected the mistake. Thanks! Having the two series of books... – paercebal Sep 04 '10 at 21:39
3
  • Faster: printf
  • More typesafe and extensible: cout
  • Better: depends! I like printf more.

I'm not alone in thinking that the way C++'s cout does formatting is just epic fail.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212