0

I read the answers on Significance of ios_base::sync_with_stdio(false); cin.tie(NULL); which basically states significance of ios_base::sync_with_stdio(false); which matters when we mix the usage of cout and printf() in C++ and helps to speeds up time, but would it still speed up time if we won't use printf ?

My understanding says no because we are not passing any instruction of printf to buffer .

simplekind
  • 39
  • 4
  • C and C++ are two differents languages please don't tag C for C++ question – Ôrel Sep 20 '21 at 18:42
  • Hard to tell. The compiler can ensure that one translation unit in a C++ program makes no use of `printf`, but it can't ensure that other translation units also don't. In the general case the compiler cannot guarantee the absence of such calls in the future and eliminate the need for synchronization while compiling the current file. – user4581301 Sep 20 '21 at 18:56
  • @user4581301, yeah, that would require an explicit LTO pass for that express purpose. I have not personally encountered a compiler that behaves this way, but who knows? Adding such a thing sounds like a low-ish effort/high-impact feature, so I wouldn't be surprised if one pops up sooner or later. –  Sep 20 '21 at 19:04
  • @Frank Agreeing back. Compiler can't do it but linker could. Also haven't seen it in the wild, but I could have and not noticed. In the stuff I write if the time spent on input cripples the performance of the program, I've already screwed up bigtime somewhere else. – user4581301 Sep 20 '21 at 19:10
  • Actually, Since this probably interacts with the OS, I was wondering it there might be stuff that would get elided that could technically qualify as side-effects, which let me to libc++'s [implementation](https://github.com/llvm/llvm-project/blob/55f0b337087136554122f942fea951a357bc4a49/libcxx/src/ios.cpp#L433) which is... interesting... –  Sep 20 '21 at 19:12
  • 1
    Performance questions are probably best answered by profiling an optimized build of your project. Try it both ways, with several runs. Measure. Then decide. – Eljay Sep 20 '21 at 19:23
  • Taking @Eljay 's idea , I wrote program to see timings cout with some stuff and endl 29.329000 ms with ios_base::sync_with_stdio(false); and cout with some stuff and endl 59.249600 ms without ios_base::sync_with_stdio(false); but i still want to know why timing is effected even there is no printf to have sync – simplekind Sep 21 '21 at 05:47
  • 1
    If the C++ streaming cout I/O is sync'd with the C style stdout I/O, it's doing that sync'ing work whether or not the C style stdout I/O is being used. – Eljay Sep 21 '21 at 11:33

0 Answers0