保存字典<String, Int32>在 C# 中 - 序列化

Saving a Dictionarylt;String, Int32gt; in C# - Serialization?(保存字典lt;String, Int32gt;在 C# 中 - 序列化?)

本文介绍了保存字典<String, Int32>在 C# 中 - 序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 C# 应用程序,该应用程序需要在启动时读取大约 130,000 个(字符串,Int32)对到字典.这些对存储在 .txt 文件中,因此任何人都可以轻松修改,这在上下文中是危险的.我想问一下有没有办法保存这个字典,以便信息可以合理安全地存储,而不会在启动时失去性能.我曾尝试使用 BinaryFormatter,但问题是,虽然原始程序在启动时需要 125 毫秒到 250 毫秒才能从 txt 读取信息并构建字典,但反序列化生成的二进制文件最多需要 2 秒,这本身并不算多,但与原始性能相比,速度降低了 8-16 倍.

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.

注意:加密很重要,但最重要的应该是一种从磁盘(可能从二进制文件)保存和读取字典的方法,而不必在每一行都使用 Convert.ToInt32,从而提高性能.

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.

推荐答案

有趣的问题.我做了一些快速测试,你是对的 - BinaryFormatter 出奇的慢:

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

  • 序列化 130,000 个字典条目:547ms
  • 反序列化 130,000 个字典条目:1046ms

当我使用带有逗号分隔值的 StreamReader/StreamWriter 对其进行编码时,我得到:

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

  • 序列化 130,000 个字典条目:121ms
  • 反序列化 130,000 个字典条目:111ms

但后来我尝试只使用 BinaryWriter/BinaryReader:

But then I tried just using a BinaryWriter/BinaryReader:

  • 序列化 130,000 个字典条目:22ms
  • 反序列化 130,000 个字典条目:36ms

代码如下:

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.

这篇关于保存字典&lt;String, Int32&gt;在 C# 中 - 序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:保存字典&lt;String, Int32&gt;在 C# 中 - 序列化

基础教程推荐