How can I invalidate the file system cache?

后端 未结 5 1777
我在风中等你
我在风中等你 2020-11-29 22:25

I want to measure/optimize the "cold boot" startup performance of an application, and it\'s difficult to do this without an actual reboot, which is obviously not a

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 23:19

    This solution worked great: Clear file cache to repeat performance testing

    More specifically, I'm doing this:

    // Open with FILE_FLAG_NO_BUFFERING
    auto hFile = CreateFile(path.c_str(),
                            GENERIC_READ,
                            FILE_SHARE_READ,
                            nullptr,
                            OPEN_EXISTING,
                            FILE_FLAG_NO_BUFFERING,
                            nullptr);
    
    /// Check
    if (hFile == INVALID_HANDLE_VALUE){
        //_tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]);
        cout << "error" << endl;
        return;
    }
    
    // Close
    CloseHandle(hFile);
    
    // Now open file with regular C++ API, and caching disabled
    ifstream file(path, ios::binary | ios::ate);
    

提交回复
热议问题