| | |
C++ advice (scanf/printf)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I just ran Narue's test on my XP, 2.41 GHz AMD Processor, 1 Gig RAM and pretty fast video card.
compiled with Visual C++ 2005 Express editiion in release mode. One problem with such as test is variance in speed of hardware -- you will get much different results with different video cards and processor speed. That alone will invalidate any such speed difference tests.
Using Microsoft compilers, I am not at all convinced that std::cout is faster than printf(). I have made similar tests in the past on other computers with M$ os and have never seen one where cout is faster than printf(). And I would be supprised if *nix computers was any different.
•
•
•
•
printf: 9.75
cout: 10.359
Using Microsoft compilers, I am not at all convinced that std::cout is faster than printf(). I have made similar tests in the past on other computers with M$ os and have never seen one where cout is faster than printf(). And I would be supprised if *nix computers was any different.
>That alone will invalidate any such speed difference tests.
Such is the way with any benchmark, but since brahle felt the need to post a speed test that proved his point (though I still can't imagine how, since he didn't have any timing framework and didn't post any profiler output), I did the same.
>I have made similar tests in the past on other computers
>with M$ os and have never seen one where cout is faster than printf().
Yes, in the past. And you don't seem to take note that quality of implementation is a huge factor. Many implementations of cout would just be a wrapper around some variant of printf, which, with the added overhead of a sentry object and various stream tests, would cause cout to be significantly slower than printf. That's where the misconception that printf is and always will be faster than cout came from.
However, on modern implementations, library writers are finally starting to realize that they can take advantage of several optimizations that can make cout *faster* than printf. We're starting to see those optimizations, and with a good implementation, such as STLPort (IIRC), cout beats printf almost every time, and that gap will only increase as implementors get better at taking advantage of C++.
>And I would be supprised if *nix computers was any different.
Then you've obviously already made up your mind, much like brahle.
Ah well, at least I tried. :rolleyes:
Such is the way with any benchmark, but since brahle felt the need to post a speed test that proved his point (though I still can't imagine how, since he didn't have any timing framework and didn't post any profiler output), I did the same.

>I have made similar tests in the past on other computers
>with M$ os and have never seen one where cout is faster than printf().
Yes, in the past. And you don't seem to take note that quality of implementation is a huge factor. Many implementations of cout would just be a wrapper around some variant of printf, which, with the added overhead of a sentry object and various stream tests, would cause cout to be significantly slower than printf. That's where the misconception that printf is and always will be faster than cout came from.
However, on modern implementations, library writers are finally starting to realize that they can take advantage of several optimizations that can make cout *faster* than printf. We're starting to see those optimizations, and with a good implementation, such as STLPort (IIRC), cout beats printf almost every time, and that gap will only increase as implementors get better at taking advantage of C++.
>And I would be supprised if *nix computers was any different.
Then you've obviously already made up your mind, much like brahle.
Ah well, at least I tried. :rolleyes:
I'm here to prove you wrong.
•
•
•
•
Originally Posted by Ancient Dragon
Using Microsoft compilers, I am not at all convinced that std::cout is faster than printf(). I have made similar tests in the past on other computers with M$ os and have never seen one where cout is faster than printf(). And I would be supprised if *nix computers was any different.
I will agree that when writing c++ program use c++ stream classes and not resort to C. There are many benefits to using streams than just a few micro-nanoseconds speed. I wouldn't have even posted in this thread had I not read Narue's test and tried it on my own computer. I haven't used STLPort or boost libraries, so no comment there.
>I will agree that when writing c++ program use c++ stream classes and not resort to C.
But we all knew that. It's just that Brahle didn't.
And his justification for using printf was rather poor. Coding competitions, he said. Pfft, where apparently the code is judged on it's time from execution to end and nothing else.
Not on its content cos that would be just silly? And hell Brahle even goes on to say that in these coding competitions he has to handle data in the order of tens of millions. And that's of course where using printf instead of cout makes a difference? :rolleyes:
Nonsense, nonsense, nonsense.
I believe Brahle lives in a world of make belief, where all kinds of non-existent characters dwell, like elves, pixies and eskimos.
:lol:
But we all knew that. It's just that Brahle didn't.
And his justification for using printf was rather poor. Coding competitions, he said. Pfft, where apparently the code is judged on it's time from execution to end and nothing else.
Not on its content cos that would be just silly? And hell Brahle even goes on to say that in these coding competitions he has to handle data in the order of tens of millions. And that's of course where using printf instead of cout makes a difference? :rolleyes:
Nonsense, nonsense, nonsense.
I believe Brahle lives in a world of make belief, where all kinds of non-existent characters dwell, like elves, pixies and eskimos.
:lol:
*Voted best profile in the world*
Narue, I am amazed at the depth and breadth of your C/C++ knowledge. You truly make this board worth reading.
On a different note, I think I need to break the rust off my C++ skills. Your code makes me realize that C is not the be-all end-all of programming languages.
On a different note, I think I need to break the rust off my C++ skills. Your code makes me realize that C is not the be-all end-all of programming languages.
"Thou shalt not follow the Null Pointer, for Chaos and Madness await thee at its end."
- Henry Spencer
- Henry Spencer
•
•
•
•
Originally Posted by Narue
>C is not the be-all end-all of programming languages.
Blasphemy!

Still, spending the last five years writing low-level C code has given me a strong affinity for the language. K&R were right - C is a small language, and is best served by a small book.
"Thou shalt not follow the Null Pointer, for Chaos and Madness await thee at its end."
- Henry Spencer
- Henry Spencer
>That K&R book with white cover is grossly overrated.
I'd like to disagree. I'm on my second personal copy because the first one fell apart due to excessive thumbing through the pages, and the (rather good) binding is starting to come apart on my second copy as well. I have a large number of books on C, which I've read cover to cover, and I can safely say (barring the C standard itself) that I value K&R the most.
I'd like to disagree. I'm on my second personal copy because the first one fell apart due to excessive thumbing through the pages, and the (rather good) binding is starting to come apart on my second copy as well. I have a large number of books on C, which I've read cover to cover, and I can safely say (barring the C standard itself) that I value K&R the most.
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Simulation of Main memory compression
- Next Thread: Need help with CPU scheduling algorithms
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





