4 ms·
What is the reason that `peek` is inefficient? Is it just because it is a function call that is not inlined or is there another reason?
by Gabriel439 9y ago
What is the reason that `peek` is inefficient? Is it just because it is a function call that is not inlined or is there another reason?
- Veedrac 9y agoReally it's the other way around: peek is complex so the compiler didn't inline it. In essence, you do a virtual call which I think winds you up in https://github.com/gcc-mirror/gcc/blob/1cb6c2eb3b8361d850be8e8270c597270a1a7967/libstdc%2B%2B-v3/include/bits/istream.tcc#L620 https://github.com/gcc-mirror/gcc/blob/1cb6c2eb3b8361d850be8... which itself redirects to a whole bunch of stuff. The virtual call alone is a lot more expensive than a simple pointer dereference, and though it's possible proper inlining might have alleviated some of this problem, you're not particularly likely to see it come close to something that's efficient by design.
- tjalfi 9y agoiostream generally has poor performance[0]. TR 18015[1] (Technical Report on C++ Performance) lists some of the implementation challenges. [0] https://stackoverflow.com/questions/4340396/does-the-c-standard-mandate-poor-performance-for-iostreams-or-am-i-just-deali https://stackoverflow.com/questions/4340396/does-the-c-stand... [1] http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf