40.0k views
0 votes
Implement a program to measure the impact of application-level buffer sizes on read time. This involves writing to and reading from a large file (say, 2 GB). Vary the application buffer size (say, from 64 bytes to 4 KB). Use timing measurement routines (such as gettimeofday and getitimer on UNIX) to measure the time taken for different buffer sizes. Analyze the results and report your findings: does buffer size make a difference to the overall write time and per-write time?

User Janette
by
4.3k points

1 Answer

2 votes

Answer:

Input file: input.txt

Output file: output.txt

input.txt is a text file of 500 MB.

CODE:

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#include <fcntl.h>

#include <sys/time.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

double count = 0; // to keep the count of writes

// function which reads from input.txt and writes to output.txt

void fun(int* file_in, int* file_out, int BUFF_SIZE)

{

char *buff = (char *)malloc(BUFF_SIZE*sizeof(char));

while(read(*file_in, buff, BUFF_SIZE) != 0)

{

write(*file_out, buff, strlen(buff));

count++;

}

}

int main(int argc, char* argv[])

{

if(argc == 1)

{

printf("Usage: ./program.c [ buff_size(in bytes) ]\\");

return 0;

}

int file_in,file_out; // input and output file pointers

file_in = open("input.txt",O_RDONLY);

if(file_in == -1)

{

printf("Error while open input.txt \\");

return 0;

}

file_out = open("output.txt",O_WRONLY);

if(file_out == -1)

{

printf("Error occured while opening output.txt\\");

return 0;

}

struct timeval t1, t2; // for capture time

double elapsedTime;

// start timer

gettimeofday(&t1, NULL);

// do something

// ...

fun(&file_in, &file_out, atoi(argv[1]));

// stop timer

gettimeofday(&t2, NULL);

// compute and print the elapsed time in millisec

elapsedTime = (t2.tv_sec - t1.tv_sec)*1000.0; // sec to ms

printf("Overall write time: %lf ms\\",elapsedTime);

printf("Per write time: %lf ms\\", elapsedTime/count);

return 0;

Step-by-step explanation:

Please see attachment for output

Implement a program to measure the impact of application-level buffer sizes on read-example-1
Implement a program to measure the impact of application-level buffer sizes on read-example-2
Implement a program to measure the impact of application-level buffer sizes on read-example-3
Implement a program to measure the impact of application-level buffer sizes on read-example-4
User Werner Lehmann
by
4.3k points