However, cout can be useful for overloading. Can you please link the SO page? The issue with printf is quite the opposite; it isn't type-safe and requires parsing of strings for types at runtime. It isn't really a big deal imo. Last edited on Oct 18, at am UTC. But for now, I strongly advise sticking to cout. However, there's a few points you should take into account:.
This means that your program can't crash because you passed the wrong number of arguments. This can happen with printf. Iostreams don't have native support for format strings which is the major root cause of 1 and 2.
This is generally a good thing, but sometimes they're useful. The Boost format library brings this functionality to Iostreams for those who need it with defined behavior throws an exception rather than undefined behavior as is the case with printf. This currently falls outside the standard. Iostreams, unlike their printf equivilants, can handle variable length buffers directly themselves instead of you being forced to deal with hardcoded cruft.
I was wondering why the std::cout call was so much more expensive and even went so far as to echo out the same string on each copy to get a better comparison on the calls. I did multiple runs to even out the comparison, but the 4x difference persisted. Then I found this answer on stackoverflow.. Switching on buffering for stdout did the trick, now my throughput numbers for printf and std::cout are pretty much the same.
I have not dug any deeper into how printf and cout differ in console output buffering, but setting the output buffer before I begin writing to the console solved my problem. Why is snprintf faster than ostringstream or is it? Note, however, that the benchmarks discussed were for formatting to memory buffers. In practical terms I have always found printf to be faster than cout. But then again, cout does a lot more for you in terms of type safety. Also remember printf is a simple function whereas cout is an object based on a complex streams hierarchy, so it's not really fair to compare execution times.
There are many improvements made for cout that you may benefit from. Why don't you do an experiment? The short URL for my code is below:. Yes, I did use the word dumb. I first made it for myself, thinking that the results were crazy, so I searched it up, which ended up with me posting my code. If you ever need to find out for performance reasons, something else is fundamentally wrong with your application - consider using some other logging facility or UI ;.
If you are running on Windows only, the non-standard cprintf might be faster as it bypasses a lot of the streams stuff.
However it is an odd requirement. Nobody can read that fast. Why not write output to a file, then the user can browse the file at their leisure?
Anecdotical evidence: I've once designed a logging class to use ostream operators - the implementation was insanely slow for huge amounts of data. I didn't analyze that to much, so it might as well have been caused by not using ostreams correctly, or simply due to the amount of data logged to disk.
I agree with the other replies that in most cases, it doesn't matter. Thousands of lines scrolling by within milliseconds isn't very informative anyway. You should never need to ask this question, as the user will only be able to read slower than both of them.
How are we doing? Please help us improve Stack Overflow. Take our short survey. Kyle Rosendo Kyle Rosendo Marcelo Cantos Marcelo Cantos k 37 37 gold badges silver badges bronze badges. Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. How can I know nobody modify global cout this way in some foreign library thread? You can easily change printf to a file as well by replacing it with fprintf Two points not otherwise mentioned here that I find significant: 1 cout carries a lot of baggage if you're not already using the STL.
Bill Weinman Bill Weinman 1, 14 14 silver badges 11 11 bronze badges. Daniel Daniel 6 6 silver badges 5 5 bronze badges. Also, to add about the performance thing, I'd say that you shouldn't output anything at all if your application is made for performance. Any sort of output to std is rather expensive and slow. I say you should avoid it and only output when it is absolutely necessary to do so. With the output operator, you have exactly one location that needs to be friend to your class, and now you can output it anywhere, even in code you didn't know about.
LuP LuP 69 3 3 bronze badges. This is not an asnwer to the question, it's more like an answer to Daniel's and Thomas's. Apollo Apollo 1, 2 2 gold badges 17 17 silver badges 25 25 bronze badges. I just reproduced and found both xyz and ABC in the output. I don't know how cout works with threads, but I know for sure that the code you are showing isn't the one that you used to get those outputs.
Please fix it, because right now it's confusing. Federico klez Culloca Fixed the issue. The function names had been reversed: cout was used with the printf syntax, and printf was used with the cout syntax.
Shouldn't have even been accepted! Although this is certainty not the best answer, I don't understand how scatman is being punished for his answer only because it was picked as the best answer. I'm not saying xbit should be down voted any more, but I don't see it being fair to down vote scatman for the OP's mistake anymore than it has to be Show 6 more comments. For printf I see: Easier, or at least shorter in term of characters written complex formatting.
For same reason as last argument. Bertrand Marron The compiler has no type information for varargs functions, so it won't convert the actual parameter except default argument promotions , such as standard integral promotions.
See 5. More differences: "printf" returns an integer value equal to the number of characters printed and "cout" does not return anything And. Keith Thompson k 39 39 gold badges silver badges bronze badges. And in what sense is the printf call "atomic"? It is like an atomic bomb. That is the point of the 'atomic bomb' statement. The memory range '7' a pointer is not usually valid; a segmentation fault could be lucky.
On some systems, '7' might print a lot of garbage to a console and you would have to look at it for a day before the program stops. In other words, this is a bad thing about printf. Static analysis tools can catch many of these issues. While technically printf doesn't do typechecking, I've never ever used a compiler that didn't warn me about type errors with printf In said task, we had to write some config to RAM.
Here are the results: C program: real 0m 8. Remeber, YMMV. Wesley Wesley 5 5 silver badges 18 18 bronze badges. A fair comparison would spread out the C into separate printfs, one for reach line. However, the example I provided in C was in consideration of performance.
If I were to make the C code readable then it might be even slower. I had no proof of separated calls to fprintf would be faster than one single call, but the reason I did it this way probably indicates that it wasn't. It could be because of locking in the C calls. Daniel Woodard Daniel Woodard 43 2 2 bronze badges.
Konrad Borowski I did a roll-back because, although the answer itself may be wrong, it is still a genuine answer. If you correctly think the answer is wrong, you have two options: 1 add a comment or 2 add a new answer or do both. Don't change someone's answer to such that it says something completely different from what was intended by the author.
Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Let us create a dummy input file containing a line with 16 bytes followed by a newline and having such lines, making a file of 17MB should be good enough.
Why is scanf faster than cin? On a high level both of them are wrappers over the read system call, just syntactic sugar. The only visible difference is that scanf has to explicitly declare the input type, whereas cin has the redirection operation overloaded using templates. This does not seem like a good enough reason for a performance hit of 5x.
With synchronization turned off, using cin and scanf together will result in an undefined mess.
0コメント