[英]How to read large data from file into MS chart control series in C#
我是C#編程的初學者。
我想讀取大的(> 1GB)文本文件(在一列中包含電壓讀數的值)並將其顯示在MS控制圖上。
它可以處理較小的文件(〜50mb),但會卡在大於300mb的文件上,甚至會為較大的文件提供內存不足的異常。
我每個人都有〜30e6個值,例如:“ 0.189312433308071”。
我現在是如何讀取數據的:
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
string line;
int pointIndex = 0;
while ((line = sr.ReadLine()) != null)
{
dataVoltage.Add(line);
chart1.Series["Default"].Points.AddXY(pointIndex, Convert.ToDouble(line));
pointIndex=pointIndex+1;
}
sr.Close();
我如何成功完成此操作而無需等待幾分鍾即可加載文件或完全不加載文件?
謝謝。
我認為您的性能問題完全在於為圖表添加值。 我使用main的第一行創建了一個中端文件(〜560mb),然后讀取它並對其值求和。 不到一分鍾。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LargeReadWrite
{
class Program
{
const string fileName = "D:\\PersonalDev\\stackoverflow\\largefile.txt";
static void Main(string[] args)
{
CreateLargeFile();
ReadLargeFile();
}
static void CreateLargeFile()
{
Random r = new Random();
using (StreamWriter sw = new StreamWriter(fileName))
{
for (uint i = 0; i < 30000000; i++)
{
sw.WriteLine(r.NextDouble());
}
}
}
static void ReadLargeFile()
{
using (StreamReader sr = new StreamReader(fileName))
{
double total = 0.0;
while (sr.Peek() >= 0)
{
total += Double.Parse(sr.ReadLine());
}
Console.WriteLine(total);
}
}
}
}
運行一些性能分析工具,並確定圖表是否適合保存此圖表,因為保存許多值將消耗您需要保存的內存。 您最好對每n個讀數的結果平均值進行分類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.