所以在我的作业中,我正在测试复制不同复制功能所需的时间。其中一个我对结果有点好奇。在我的副本功能中,它涉及分配内存,如下所示:
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!
}
主程序允许用户输入infile outfile copyFunctionNumber。如果选择3,则用户可以输入特定的缓冲区大小。所以我正在测试复制具有不同缓冲区大小的文件(6.3 MB)。当我选择1024时,它给出了42,000微秒的差异,对于2000它给出了26,000微秒,但对于3000它给出了34,000微秒。我的问题是为什么要重新上线?你怎么能告诉复制的完美缓冲区大小花费最少的时间。