开发者

Windows phone's fastest read from isolated storage

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 fa开发者_JAVA百科ster. But what is the optimal size of these chunks? Have anyone done such tests?


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
{
    //...
}


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


(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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜