0

I know this is kind of a generic question, but I'm not sure how to handle it nonetheless.

I am writing some tests to gather data about how long it takes to open a file and pull some data out of it using two different methods. I'm noticing that in some cases the difference can be pretty dramatic, and that might be the valid result, but I want to eliminate potential mitigating factors. In particular, I'm worried about a "warm" vs. "cold" file open on Windows.

My basic process here (pseudo-ish code) is as follows:

private void GatherMetrics(List<FileInfo> files)
{
    // Each metric holds how much data was gathered and how long it took
    var results = new List<MetricsResult>();

    for (int i = 0; i < files.Count; i++)
    {
        results.Add(GetAllData(files[i]));
        results.Add(GetDataSubset(files[i]));
    }

    // This just generates statistics on the data and outputs it somewhere.  Not
    // really relevant, since by then the data is already gathered.
    LogResults(results);
}

private MetricsResult GetAllData(FileInfo file)
{
    var stopwatch = new Stopwatch();
    stopwatch.Start();

    // Open file
    // Get the data from the file
    // Close file

    stopwatch.Stop();

    return new MetricsResult 
    { 
        Time = stopwatch.ElapsedMilliseconds,
        Data = /* data gathered from the file */
    }
}

private MetricsResult GetAllData(FileInfo file)
{
    // Same as GetAllData, only gathers data in a different way
}

As you can see, I'm opening a file, getting the data, closing the file, opening it again, getting the data in a different way, and closing it again.

My question is this: is there a way to force the file out of memory, so I don't get affected by OS caching of any sort? I'm dealing with a lot of files (>4000), so the only thing I can think of is to process all of the files with one method, then start over from the beginning and do it again with the other method. That gives me the greatest chance of having the first file pass out of memory by the time I get back around to that.

Or is there a better way to handle this?

This might not be a C#-specific question, but I'll tag it as C# anyway since that's what I'm using to do all of this. C# answers would be most directly helpful, but since this question is half-conceptual, anything would do, really.

Ari Roth
  • 5,392
  • 2
  • 31
  • 46

1 Answers1

0

I believe if you are using windows, you can open the file using the FILE_FLAG_NO_BUFFERING flag. More information about the flag can be found here, and there was a related stack overflow question here that might provide some more useful information