cin and cout speed up

Add the following two sentences at the beginning of the main function main, which can greatly increase the speed of cin and cout:

#include <iostream>
int main() {
	ios::sync_with_stdio(false);
	cout.tie(NULL);
	return 0;
}

Intuitively feel the performance improvement of C ++ reading by adding these two sentences (it seems faster than using scanf): the
Before speeding up
After speeding up
speed has been increased by nearly 2 times , the principle is that C / C ++ has its own file buffer, in order to prevent their own The buffer is misaligned, and the buffers of the C function and the C ++ function are synchronized by default, so the cin overhead in C ++ becomes larger. These two statements cancel the buffer synchronization .
However, this method also has a disadvantage. It may happen when scanf and cin are mixed, or cout and printf are mixed due to the cancellation of synchronization 错误. Like this: Insert picture description here
Therefore, when using the above statement, it is best to use only cin and cout

Published 24 original articles · praised 3 · visits 1732

Guess you like

Origin blog.csdn.net/qq_38507937/article/details/105495399