16

I've made some programs and saw that scanf and printf are considerably faster than using cin and cout? Most of my programs clear the execution time limit, mostly 3 seconds or 5 seconds, on online compilers when using scanf/printf which exceeded the limit while using cin/cout.

Yu Hao
  • 111,229
  • 40
  • 211
  • 267
shashankg77
  • 1,866
  • 3
  • 16
  • 32
  • You say "clear the time limit on online compilers" which makes me think you actually meant to ask why do programs that use scanf/printf *compile* faster than ones that use cin/cout. Is this correct? – Jason C Aug 05 '13 at 00:16
  • If you care about speed, cid/cout are not your friend. You need to be using more specialist instruments. This is discussed in Effective C++ among others. – Jack Aidley Aug 05 '13 at 00:22
  • I believe it's also worth noting that if you care about speed and you've determined that your code is not meeting your performance requirements, you should first determine *where* your bottleneck is before attempting to optimize it. If your bottleneck does not involve iostream vs stdio at all, then your energy would be more well spent on other parts of your code. – Jason C Aug 05 '13 at 00:27
  • 1
    @JackAidley: If you read the linked questions you will find cin/cout are as fast as C stdin/stdout if you first take the time to decouple them from the C infrastructure. – Martin York Aug 05 '13 at 03:59
  • see also this question and answers : http://stackoverflow.com/questions/18688763/why-is-istream-ostream-slow – Muhammad Annaqeeb Jan 31 '14 at 23:10

0 Answers0