1

我有以下课程:

public class HPChartGraphPoint
{
    public int Keyword { get; set; }
    public List<long> Points { get; set; }

    public HPChartGraphPoint()
    {
        Points = new List<long>();
    }


    public string SerializeToString()
    {
        var sb = new StringBuilder();

        sb.Append(Keyword);
        sb.Append(":");

        for (int i = 0; i < Points.Count; i++)
        {
            sb.Append(Points[i]);
            sb.Append(":");
        }
        return sb.ToString();
    }

    public static HPChartGraphPoint DesirializeFromString(string str)
    {
        var splitter = str.Split(new[]{":"},StringSplitOptions.RemoveEmptyEntries)
        .Select(x => int.Parse(x)).ToArray();

        var retPoint = new HPChartGraphPoint();

        retPoint.Keyword = splitter[0];

        for (int i = 1; i < splitter.Length; i++)
        {
            retPoint.Points.Add(splitter[i]);
        }

        return retPoint;
    }


}

我需要存储和加载包含数百万个元素的集合列表。如何做到又快又省内存?

编辑:

这是用于存储的代码

using (var sw = new StreamWriter(filePath))
{
                for (int i = 0; i < HPChartGraphPointCollection.Count; i++)
                {
                    sw.WriteLine(HPChartGraphPointCollection[i].SerializeToString());
                }

}
4

1 回答 1

0

二进制格式可能比文本更紧凑,更容易阅读/解析。

您可以使用轻量级流包装器:​​ BinaryReaderBinaryWriter

于 2012-06-22T20:10:53.100 回答