Fast I/O for Reading Input.

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
baodog
Experienced poster
Posts: 202
Joined: Wed Jul 04, 2007 6:53 am

Fast I/O for Reading Input.

Post by baodog »

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.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

sscanf() always converts the string, starting from its beginning. You should use something like the following code to parse several integers with it:

Code: Select all

int x, cnt;
for (int pos = 0; sscanf(buf+pos, "%d%n", &x, &cnt) != 0; pos += cnt) {
  process integer x;
}
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.
Also, is anyone willing to share their GCC ASM for copying file
to memory fast
I'm just using read() like you do.
Post Reply

Return to “C”