我们在课堂上一直在使用gettimeofday
.
令人惊讶的是,经过多次重复(每次执行 3 次测量,执行 3 次)后,我们发现写入 2500MB 的文件比在两个 HDD 上写入较小的文件要快。
这是我们的 C 代码。它从 python 脚本调用以生成一些图表。
//argv[1] = path, argv[2] = size in MB (2500 in this case)
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
struct timeval tv0;
struct timeval tv1;
int main(int argc, char *argv[]){
unsigned long size=atoi(argv[2])*1000L*1000L;
int f = open(argv[1], O_CREAT|O_WRONLY|O_TRUNC, 0777);
char * array = malloc(size);
gettimeofday(&tv0, 0); //START TIME
write(f, array, size);
fdatasync(f);
close(f);
gettimeofday(&tv1, 0); // END TIME
double seconds = (((double)tv1.tv_sec*1000000.0 + (double)tv1.tv_usec) - ((double)tv0.tv_sec*1000000.0 + (double)tv0.tv_usec))/1000000.0;
printf("%f",seconds);
}
老师不知道,所以我在这里问:有可能发生这种情况吗?