您好我写了一个简单的代码来读取大型(10GB)二进制文件到内存并测量kvm的访客virtual machine
中的运行时间。
大文件由fallocate -l 10GB test.bin
四个结果
案例1.第一次或[shutdown->boot]
之后:17s
案例2.第二次:4s
案例3.清除缓存缓冲区后[通过运行sync && echo 3 > /proc/sys/vm/drop_caches
或rm file and recreate file
]:6s
案例4.重启后:13s
我认为案例1,3,4应该得到相同的结果,因为缓冲区缓存已被清除,但为什么它们现在得到不同的结果?
这是代码段
int test()
{
int ret = 0;
streampos size;
char* buffer;
//define file pointer pointed to a large_file
ifstream large_file("test.bin", ios::in|ios::binary|ios::ate);
if(large_file.is_open()){
size = large_file.tellg();//get the size of large_file
buffer = new char[size];//allocate memory
large_file.seekg(0, ios::beg);//set get position to the begin of the file
start = wtime();
large_file.read(buffer, size);
large_file.close();
end = wtime();
printf("File size = %ldGB Time = %.5f\n", size/1024/1024/1024, end-start);
delete buffer;
return ret;
}
else printf("failed to open the file\n");
ret = 1;
return ret;
}