保存字典<字符串的Int32>在C#中 - 序列化? [英] Saving a Dictionary<String, Int32> in C# - Serialization?

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

问题描述

早上好

我写需要读13万(字符串,Int32)将在启动时对字典的一个C#应用程序。在对存储在.txt文件,并因此很容易地修改任何人,这点是在上下文危险。我想问一下,如果有一种方法来保存这本字典,这样可以合理安全地存储的信息,在启动时不损失性能。我已经尝试使用的BinaryFormatter ,但问题是,虽然原来的方案需要高炮为125ms,并在启动时250毫秒,从TXT读取信息,并建立了字典,反序列化所产生的二进制文件最多需要2秒,这是不是太本身,但比原来的性能时,在速度8-16x下降。

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 beteen 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:


  • 序列化130000词典条目: 547ms

  • 反序列化130000词典条目: 1046ms

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

当我用一个StreamReader / StreamWriter的codeD将其与逗号分隔值我:

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


  • 序列化130000词典条目: 121ms

  • 反序列化130000词典条目: 111ms

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

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

But then I tried just using a BinaryWriter/BinaryReader:


  • 序列化130000词典条目: 22ms内

  • 反序列化130000词典条目: 36ms

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

在code表示如下:

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;字符串的Int32&GT;在C#中 - 序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆