which is faster, and which is more flexible: printf or cout? [duplicate]
Asked Answered
B

2

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.

Blazon answered 4/9, 2010 at 20:6 Comment(11)
@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.Weigela
Duplicate of: https://mcmap.net/q/271554/-cin-or-printfTaffeta
@Michael Petrotta: you obviously didn't read my question fully, i'm not asking one which is better.Blazon
"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.Taffeta
@Greg, Michael: I agree with Michael. The answers cover the information you are looking for, even if the question doesn't.Taffeta
@Greg: I did read your question, and that's exactly what you're asking. Or are speed and flexibility not good things?Weigela
Both are a lot faster than a human can read.Oujda
@Michael: I was directing at your first comment, which was "Possible duplicate of: https://mcmap.net/q/73746/-39-printf-39-vs-39-cout-39-in-c" which was what I linked to. It looks like you've deleted it, or editted it out.Blazon
@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.Weigela
"closed as exact duplicate": I don't agree it is an exact duplicate, but I had no idea https://mcmap.net/q/271554/-cin-or-printf was actually there/Blazon
@Greg: It's a duplicate of printf vs cout C++.Dynamometry
F
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).

Figment answered 4/9, 2010 at 20:10 Comment(2)
Effective C++ was written by Scott Myers. Herb Sutter wrote Exceptional C++. Yes, the names are similar...Lincolnlincolnshire
@Whisty: Right! I corrected the mistake. Thanks! Having the two series of books...Figment
U
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.

Unemployed answered 4/9, 2010 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.