0

So in my assignment I am testing the times it takes for different copying functions to copy. One of them I am a bit curious on the results. In one my copy functions it involves allocating memory like so:

int copyfile3(char* infilename, char* outfilename, int size) {
  int infile; //File handles for source and destination.
  int outfile;

  infile = open(infilename, O_RDONLY); // Open the input and output files.
  if (infile < 0) {
    open_file_error(infilename);
    return 1;
  }

  outfile = open(outfilename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (outfile < 0) {
    open_file_error(outfilename);
    return 1;
  }

  int intch;  // Character read from input file. must be an int to catch EOF.
  char *ch = malloc(sizeof(char) * (size + 1));

  gettimeofday(&start, NULL);

  // Read each character from the file, checking for EOF.
  while ((intch = read(infile, ch, size)) > 0) {

    write(outfile, ch, intch); // Write out.
  }

  gettimeofday(&end, NULL);

  // All done--close the files and return success code.
  close(infile);
  close(outfile);

  free(ch);

  return 0; // Success!
}

The main program allows the user to input the infile outfile copyFunctionNumber. If 3 is chosen the user can input a specific buffer size. So I was testing copying a file (6.3 MB) with different buffer sizes. When I choose 1024 it gives a difference of 42,000 microseconds, for 2000 it gives 26,000 microseconds, but for 3000 it gives 34,000 microseconds. My question is why does it go back up? And how could you tell what the perfect buffer size will be for the copying to take the least amount of time.

Smreks
  • 317
  • 1
  • 7
  • 19
  • 2
    Did you run your benchmark a dozen of times each? The [page cache](http://en.wikipedia.org/wiki/Page_cache) matters a lot for such IO intensive benchmarks – Basile Starynkevitch Feb 27 '15 at 19:28
  • There are many algorithms for all sorts of things that will exhibit a "saddle-like" performance curve. Finding the useful minimum (or maximum) is the whole point of performance tuning, and the more variables you have, the harder it becomes to find the best combination instead of one that's just locally "good enough"... – twalberg Feb 27 '15 at 21:00
  • `char *ch = malloc(sizeof(char) * (size + 1));`: no need to allocate the extra byte, `ch` is a confusing name for a char array, `sizeof(char)` equals 1 by definition. – chqrlie Feb 27 '15 at 21:38
  • powers of 2 should give you the best performance, but careful timing will tell you if matters at all... small numbers should definitely give you poor performance. – chqrlie Feb 27 '15 at 21:41
  • If this is going to the same drive, then random accesses between reads and writes will be an issue, somewhat offset by a hard drive's look ahead and write behind cache algorithms. I'd be interested in the time it takes to copy the file with a very large buffer doing a single 6.3 MB read and a single 6.3 MB write. If the source file and destination files are on separate hard drives, or if a SSD drive is being used, then random access issues are greatly reduced. – rcgldr Feb 27 '15 at 22:54
  • Is there a reason why it takes longer to copy when giving a larger buffer size? – Smreks Feb 28 '15 at 00:41

0 Answers0