0

I need to read a file from isolated storage (IsolatedStorageFileStream) as fast as possible. Now I read it per byte. I'm sure that reading in chunks of bytes will be faster. But what is the optimal size of these chunks? Have anyone done such tests?

1
  • Seems nobody has done such tests. I'm going to do it by myself and run on emulator (just out of interest) and my HTC Trophy. Let you know about results.
    – Pashec
    Sep 8, 2011 at 9:55

4 Answers 4

1

The IO performance is going to vary based on the physical device, so there is no "magic number" answer to your question.

The short answer to your question is: read the entire file into memory at once, or if the file is too big (not likely, my phone has 576MB RAM = 1/4 my monthly download limit) then read as much data as possible at once based on available memory, process it, then read as much as possible again, etc.

// Obtain a virtual store for the application.
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

try
{
    // Specify the file path and options.
    using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.dat", FileMode.Open, myStore))
    {
        // Get the data size
        int length = (int)isoFileStream.Length;

        //TODO: Check file size vs. available memory and break up into chunks if needed

        // Create an in memory buffer
        char [] buffer = new char [length];

        // Read all of the data.
        using (var isoFileReader = new StreamReader(isoFileStream))
        {
            isoFileReader.ReadBlock(buffer, 0, length);
        }
    }
}
catch
{
    //...
}
1
  • Thanks for suggestion. However, I can't read the whole file at once because it can be too big.
    – Pashec
    Sep 8, 2011 at 9:46
1

Well, I performed test by myself. What I had: file size = 3 803 264 bytes; testing code snippet:

using (var file = storage.OpenFile("test.dat", FileMode.Open))
{
    var startTime = DateTime.Now;
    const int count = 1;
    var buffer = new byte[count];
    **long position = file.Position;
    **long length = file.Length;
    while (position < length)
    {
        file.Read(buffer, 0, count);
        position += count;
    }
    uxLog.Text = (DateTime.Now - startTime).TotalMilliseconds.ToString();
}

EDIT: very important point in above snippet is that file.Position and file.Length are requested only once. It has significant positive impact on performance (provided count=1 2370 ms against 55734 ms if position and length are inlined).

What updated results I've got:

buffer size (bytes) / time in emulator (ms) / time on my HTC Trophy (ms)
1 / 1197 / 2370
128 / 725 / 1289
1024 / 209 / 163
4096 / 35 / 50
8196 / 35 / 49
whole file at once / 19 / 31

1
  • Like I said: "read the entire file into memory at once" or "read as much data as possible at once based on available memory". Was I right? Sep 8, 2011 at 23:22
0

(Disclaimer: Not tested on Windows Phone)

Generally, reading is best performed in sufficient chunks that the operating system may preload. However, the .NET api may actually perform buffering for you, saving system calls to the kernel. The impact of this is hard to predict.

Performance is usually best around the 16,384-65,535 byte area for many systems, though can sometimes scale into the 1MiB+ range.

0

Windows CE uses a 4k page and 64k virtual memory allocation blocks, so my bet is that one of those values is going to give you the best performance. Of course the flash driver might have something to say about it too - the flash sector size will have an effect, but probably more on writes than reads as it has to erase and rewrite an entire sector, not just the byte you're changing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.