c++ stream 慢

 

以前用c++写过一个程序,读入很多行的文本,写出来后发现比最早用python readlines()的版本慢很多,后来认真比较了一下,发现确实是c++流比较慢。就改用c的fread()一次读入整个文件,这样才和python的readlines()基本差不多。

今天看到一篇文章

Why is reading lines from stdin much slower in C++ than Python?

https://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python#

有回答者说,前面加上

std::ios_base::sync_with_stdio(false);

c++ iostream也能很快。

To make cout, cin, cerr and clog faster, do it this way std::ios_base::sync_with_stdio(false); 

Note that sync_with_stdio() is a static member function, and a call to this function on any stream object (e.g. cin) toggles on or off synchronization for all standard iostream objects. – John Zwinck 

还有人说

std::ios_base::sync_with_stdio(false);
char buffer[1048576];
std::cin.rdbuf()->pubsetbuf(buffer, sizeof(buffer));

即一个大的缓冲区会更快,但也有人说不是这样。

 

if the performance is something you care about, you should really just buffer the entire file into memory (assuming it will fit).

Here's an example:

//open file in binary mode
std::fstream file( filename, std::ios::in|::std::ios::binary );
if( !file ) return NULL;

//read the size...
file.seekg(0, std::ios::end);
size_t length = (size_t)file.tellg();
file.seekg(0, std::ios::beg);

//read into memory buffer, then close it.
char *filebuf = new char[length+1];
file.read(filebuf, length);
filebuf[length] = '\0'; //make it null-terminated
file.close();

If you want, you can wrap a stream around that buffer for more convenient access like this: 

std::istrstream header(&filebuf[0], length);

Also, if you are in control of the file, consider using a flat binary data format instead of text. It's more reliable to read and write because you don't have to deal with all the ambiguities of whitespace. It's also smaller and much faster to parse.

shareimprove this answer

edited May 28 '18 at 17:55

Gaius

1,7261739

answered Mar 13 '12 at 23:04

Stu

1,1891011

 

你可能感兴趣的:(c++)