I tried to use read() to cache the input file, but it's going into infinite loop for some reason ... Anything wrong with this? Thanks. Does sscanf not work with input from read() ?
char buf[1024*1024];
long n=read(0,buf,sizeof(buf));
buf[n-1]='\0';
while(sscanf(buf, "%ld",&p)==1);
Also, is anyone willing to share their GCC ASM for copying file
to memory fast ? I crashed my machine while trying to do this.
Or maybe how I can get getc_unlocked()? Or quivalent assembly code? thanks.
Fast I/O for Reading Input.
Moderator: Board moderators
sscanf() always converts the string, starting from its beginning. You should use something like the following code to parse several integers with it:
gcc's implementation of sscanf() is also pretty slow - for some reason it needs to look through the entire string (doing strlen, or something) before parsing it, so you should write your own integer parsing routing.
Code: Select all
int x, cnt;
for (int pos = 0; sscanf(buf+pos, "%d%n", &x, &cnt) != 0; pos += cnt) {
process integer x;
}
I'm just using read() like you do.Also, is anyone willing to share their GCC ASM for copying file
to memory fast