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

  • SEARCH
  • Home
  • 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 3287562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:30:25+00:00 2026-05-17T20:30:25+00:00

I am writing a C# application that needs to read about 130,000 (String, Int32)

  • 0

I am writing a C# application that needs to read about 130,000 (String, Int32) pairs at startup to a Dictionary. The pairs are stored in a .txt file, and are thus easily modifiable by anyone, which is something dangerous in the context. I would like to ask if there is a way to save this dictionary so that the information can be reasonably safely stored, without losing performance at startup. I have tried using BinaryFormatter, but the problem is that while the original program takes between 125ms and 250ms at startup to read the information from the txt and build the dictionary, deserializing the resulting binary files takes up to 2s, which is not too much by itself but when compared to the original performance is a 8-16x decrease in speed.

Note: Encryption is important, but the most important should be a way to save and read the dictionary from the disk – possibly from a binary file – without having to use Convert.ToInt32 on each line, thus improving performance.

  • 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-05-17T20:30:26+00:00Added an answer on May 17, 2026 at 8:30 pm

    interesting question. I did some quick tests and you are right – BinaryFormatter is surprisingly slow:

    • Serialize 130,000 dictionary entries: 547ms
    • Deserialize 130,000 dictionary entries: 1046ms

    When I coded it with a StreamReader/StreamWriter with comma separated values I got:

    • Serialize 130,000 dictionary entries: 121ms
    • Deserialize 130,000 dictionary entries: 111ms

    But then I tried just using a BinaryWriter/BinaryReader:

    • Serialize 130,000 dictionary entries: 22ms
    • Deserialize 130,000 dictionary entries: 36ms

    The code for that looks like this:

    public void Serialize(Dictionary<string, int> dictionary, Stream stream)
    {
        BinaryWriter writer = new BinaryWriter(stream);
        writer.Write(dictionary.Count);
        foreach (var kvp in dictionary)
        {
            writer.Write(kvp.Key);
            writer.Write(kvp.Value);
        }
        writer.Flush();
    }
    
    public Dictionary<string, int> Deserialize(Stream stream)
    {
        BinaryReader reader = new BinaryReader(stream);
        int count = reader.ReadInt32();
        var dictionary = new Dictionary<string,int>(count);
        for (int n = 0; n < count; n++)
        {
            var key = reader.ReadString();
            var value = reader.ReadInt32();
            dictionary.Add(key, value);
        }
        return dictionary;                
    }
    

    As others have said though, if you are concerned about users tampering with the file, encryption, rather than binary formatting is the way forward.

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

Sidebar

Related Questions

No related questions found

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.