Question

Hi I write a simple code to read large(10GB) binary file to memory and measure the running time in a kvm's guest virtual machine.

Large file is created by fallocate -l 10GB test.bin

Four results

case 1. First time or after [shutdown->boot]: 17s

case 2. Second time: 4s

case 3. After clear the cache buffer[by running sync && echo 3 > /proc/sys/vm/drop_caches or rm file and recreate file]: 6s

case 4. After reboot: 13s

I think case 1,3,4 should get the same results, because the buffer cache is cleared, but why they get different results now???

here is the code snippet

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;
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top