CP Notebook

← all snippets

FastInput

Read an integer from stdin. Usage requires your program to pipe in input from file.

Time: About 5x as fast as cin/scanf. 17 lines tested on SPOJ INTEST, unit tested

Usage: ./a.out < input.txt

content/various/FastInput.h — chilli, source: Own work

inline char gc() { // like getchar()
	static char buf[1 << 16];
	static size_t bc, be;
	if (bc >= be) {
		buf[0] = 0, bc = 0;
		be = fread(buf, 1, sizeof(buf), stdin);
	}
	return buf[bc++]; // returns 0 on EOF
}

int readInt() {
	int a, c;
	while ((a = gc()) < 40);
	if (a == '-') return -readInt();
	while ((c = gc()) >= 48) a = a * 10 + c - 480;
	return a - 48;
}