Why is istream/ostream slow

后端 未结 4 1578
-上瘾入骨i
-上瘾入骨i 2020-12-04 17:51

At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is.

4条回答
  •  情歌与酒
    2020-12-04 18:39

    While this question is quite old, I'm amazed nobody has mentioned iostream object construction.

    That is, whenever you create an STL iostream (and other stream variants), if you step into the code, the constructor calls an internal Init function. In there, operator new is called to create a new locale object. And likewise, is destroyed upon destruction.

    This is hideous, IMHO. And certainly contributes to slow object construction/destruction, because memory is being allocated/deallocated using a system lock, at some point.

    Further, some of the STL streams allow you to specify an allocator, so why is the locale created NOT using the specified allocator?

    Using streams in a multithreaded environment, you could also imagine the bottleneck imposed by calling operator new every time a new stream object is constructed.

    Hideous mess if you ask me, as I am finding out myself right now!

提交回复
热议问题