Table Of Contents
Previous Section Next Section

Program 16: Slow but Sure

Why is this program so slow? It takes a minute, 34 seconds on my system to copy the file, while the Linux cp command does the same thing in less than half a second. What can be done to make the program faster?

  1 /************************************************
  2  * copy input file to output file.              *
  3  ************************************************/
  4 #include <iostream>
  5 #include <unistd.h>
  6 #include <fcntl.h>
  7
  8 int main() {
  9     // The fd of the input file
 10     int in_fd = open("file.in", O_RDONLY);
 11
 12     // The fd of the output file
 13     int out_fd = open("file.out",
 14             O_WRONLY|O_CREAT, 0666);
 15
 16     char ch;     // Character to copy
 17
 18     if (in_fd < 0) {
 19         std::cout <<
 20             "Error could not open input file\n";
 21         exit (8);
 22     }
 23
 24     if (out_fd < 0) {
 25         std::cout <<
 26             "Error could not open output file\n";
 27         exit (8);
 28     }
 29     while (1) {
 30         if (read(in_fd, &ch, 1) != 1)
 31             break;
 32
 33         write(out_fd, &ch, 1);
 34     }
 35     close(in_fd);
 36     close(out_fd);
 37     return (0);
 38 }

(Next Hint 6. Answer 96.)

Table Of Contents
Previous Section Next Section