Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:16:05+00:00 2026-06-03T17:16:05+00:00

While writing a simple library to parse a game’s data files, I noticed that

  • 0

While writing a simple library to parse a game’s data files, I noticed that reading an entire data file into memory and parsing from there was significantly faster (by up to 15x, 106s v 7s).

Parsing is usually sequential but seeks will be done every now and then to read some data stored elsewhere in a file, linked by an offset.

I realise that parsing from memory will definitely be faster, but something is wrong if the difference is so significant. I wrote some code to simulate this:

public static void Main(string[] args)
{
    Stopwatch n = new Stopwatch();

    n.Start();
    byte[] b = File.ReadAllBytes(@"D:\Path\To\Large\File");
    using (MemoryStream s = new MemoryStream(b, false))
        RandomRead(s);
    n.Stop();
    Console.WriteLine("Memory read done in {0}.", n.Elapsed);
    b = null;
    n.Reset();
    n.Start();
    using (FileStream s = File.Open(@"D:\Path\To\Large\File", FileMode.Open))
        RandomRead(s);
    n.Stop();
    Console.WriteLine("File read done in {0}.", n.Elapsed);
    Console.ReadLine();
}
private static void RandomRead(Stream s)
{
    // simulate a mostly sequential, but sometimes random, read
    using (BinaryReader br = new BinaryReader(s)) {
        long l = s.Length;
        Random r = new Random();
        int c = 0;
        while (l > 0) {
            l -= br.ReadBytes(r.Next(1, 5)).Length;
            if (c++ <= r.Next(10, 15)) continue;
            // simulate seeking
            long o = s.Position;
            s.Position = r.Next(0, (int)s.Length);
            l -= br.ReadBytes(r.Next(1, 5)).Length;
            s.Position = o;
            c = 0;
        }
    }
}

I used one of the game’s data files as input to this. That file was about 102 MB, and it produced this result (Memory read done in 00:00:03.3092618. File read done in 00:00:32.6495245.) which has memory reading about 11x faster than file.

The memory read was done before the file read to try and improve its speed via the file cache. It’s still that much slower.

I’ve tried increasing or decreasing FileStream‘s buffer size; nothing produced significantly better results, and increasing or decreasing it too much just worsened the speed.

Is there something I’m doing wrong, or is this to be expected? Is there any way to at least make the slowdown less significant?

Why is reading the entire file at once and then parsing it so much faster than reading and parsing simultaneously?

I’ve actually compared to a similar library written in C++, which uses the Windows native CreateFileMapping and MapViewOfFile to read files, and it’s very fast. Could it be the constant switching from managed to unmanaged and the involved marshaling that causes this?

I’ve also tried MemoryMappedFiles present in .NET 4; the speed gain was only about one second.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:16:08+00:00Added an answer on June 3, 2026 at 5:16 pm

    Is there something I’m doing wrong, or is this to be expected?

    No, nothing wrong. This is entirely expected. That accessing the disk is an order of magnitude slower than accessing memory is more than reasonable.


    Update:

    That a single read of the file followed by processing is faster than processing while reading is also expected.

    Doing a large IO operation and processing in memory would be faster than getting a bit from disk, processing it, calling the disk again (waiting for the IO to complete), processing that bit etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing code in a file that would comprise of PHP, HTML, CSS &
i am writing a simple function for a library, that will take in as
So, I'm writing code for a class that will go into a library that
I'm writing a web site (C#, ASP 3.5) while implementing a simple CMS. In
While writing a file using ofstream, how do I know when the file's size
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
While writing a .jsp file, can i include all the html5 tags in it
Let's say I'm writing a little library in C -- some data structure, say.
While writing a simple server-client application, this question came in my mind. When someone
I'm writing a simple web-based front-end for a Python console program that runs on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.