我有以下课程:
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());
}
}