How can i read a file, which consists on about 10M integers separated by \n.
I don't know how to use fscanf and i tried reading char by char and converting to int, but it's too slow.
Thanks.
Printable View
How can i read a file, which consists on about 10M integers separated by \n.
I don't know how to use fscanf and i tried reading char by char and converting to int, but it's too slow.
Thanks.
I don't particularly see fscanf being faster than char by char and converting to int. Any reason why this has to be insanely fast ?
But it's really slow, like 1000 int in 3 seconds.
I'm doing the sort-on-disk problem of programming pearls by the way.
I guess the next step would be to see some code. I sort of doubt it's your reading method that's causing slowdown.
This is the loop that reads the integers and adds them into a bit array.Code:while((c=getc(i)) != EOF)
{
if(isdigit(c))
{
t = (t*10) + c - '0';
} else {
if(t<x*8000000)
{
set(T,t);
}
t=0;
}
}
The code looks fine -- could be made clearer, but it looks okay. I doubt the slowness is because of this part of the code.
This is the only part executing at the time, besides i used some printf's to count the amount of numbers introduced.
Even set() is nothing really.
Code:void set(char T[],int n)
{
T[n/8] &= (128 >> (n%8));
}
>How can i read a file, which consists on about 10M integers separated by \n.
Use malloc() to create the array, then use:
And depending on whether you actually need an array, you could just read into an int.Code:long i = 0;
while (fscanf(fp, "%d", &array[i++]) == 1);
Time this
while((c=getc(i)) != EOF);
Which does nothing, except read the file.
If that's like 99% of the time you're seeing at the moment, then nothing in the loop has any impact on the performance.
15 secons to read thw whole file, which is 114MB