AJwr
AJwr

Reputation: 618

Speed up many UNIX read/write calls in C

I'm writing a program (fair warning, it's a homework problem) that will be sorting 50 million floats it receives as an input file and outputting them to another file. I am using the UNIX standard read() and write() calls to read in the input and write them to the file, however it seems to be running quite slowly because of how many of these calls there are.

Here is the relevant code for reading:

floats* input = make_floats(0); // input floats

int ifd = open(iname, O_RDONLY, 0644);

long count; // temp to get count

read(ifd, &count, sizeof(long)); // read in count

// read in numbers from input
for (int i = 0; i < count; ++i) {
    float num;

    read(ifd, &num, sizeof(float));

    floats_push(input, num);
}

it reads in the numbers one at a time into a float vector (that is the floats*).

Here is the writing:

floats* xs = make_floats(0);
int ofd = open(args->output, O_WRONLY, 0644);
int xs_counter = 0;

// write the local array to file
for (int i = start; i < end; ++i) {
    write(ofd, &(xs->data[xs_counter]), sizeof(float));
    ++xs_counter;
}

start and end are the essentially how much I'm writing. I'm wondering if I can essentially make these into a single mass read and write call, but I'm a little lost as to how (the code works fine btw it's just slow)

Thanks

Upvotes: 0

Views: 50

Answers (1)

Anthony Palumbo
Anthony Palumbo

Reputation: 314

Try replacing all those read calls with a single read:

float *numbers = malloc(sizeof(float) * count);
read(ifd, numbers, sizeof(float) * count);

Upvotes: 1

Related Questions