有一个100MB的二进制文件,里面依次存储浮点型的坐标值,如X1 Y1 X2 Y2......... (X及Y都为double类型)现在想将里面的数据解析出来,存在一个List里面,有什么快速的方法?本人试过struct PointD
{
public double X;
public double Y;
Public PointD(double x,double y)
{
X=x;Y=y;
}
}1:
FileStream fs=.....
BinaryReader br=......
List<PointD> liMyPoints=new List<PointD>();while(fs.Position<fs.Length)
{
liMyPoints.Add(new PointD(br.ReadDouble(),br.ReadDouble());
}2:byte[] buffer=br.ReadBytes(1024);
int pos=0;
while(pos<buffer.Length)
{
double x=BitConvert.ToDouble(buffer,pos);
pos+=8;
double y=BitConvert.ToDouble(buffer,pos);
pos+=8;
liMyPoints.Add(new PointD(x,y));
}以上方法细节上是有问题的,但思路是:
1:逐个读取数据类型
2:先读入一个缓冲内,再从缓冲内进行解析经过测试,2比1快了很多,但仍觉得不够快,请问有没有其他什么更好的方法来更快速地从文件中将每一个点的坐标解析出来呢?
{
public double X;
public double Y;
Public PointD(double x,double y)
{
X=x;Y=y;
}
}1:
FileStream fs=.....
BinaryReader br=......
List<PointD> liMyPoints=new List<PointD>();while(fs.Position<fs.Length)
{
liMyPoints.Add(new PointD(br.ReadDouble(),br.ReadDouble());
}2:byte[] buffer=br.ReadBytes(1024);
int pos=0;
while(pos<buffer.Length)
{
double x=BitConvert.ToDouble(buffer,pos);
pos+=8;
double y=BitConvert.ToDouble(buffer,pos);
pos+=8;
liMyPoints.Add(new PointD(x,y));
}以上方法细节上是有问题的,但思路是:
1:逐个读取数据类型
2:先读入一个缓冲内,再从缓冲内进行解析经过测试,2比1快了很多,但仍觉得不够快,请问有没有其他什么更好的方法来更快速地从文件中将每一个点的坐标解析出来呢?
2、建议你考虑一下操作系统IO的影响,即文件存储在硬盘上的块大小以及操作系统本身的IO缓存的影响。如果要精确到毫秒级,这些还是需要考虑一下的。
int pos=0;
while(pos <buffer.Length)
{
double x=BitConvert.ToDouble(buffer,pos);
pos+=8;
double y=BitConvert.ToDouble(buffer,pos);
pos+=8;
liMyPoints.Add(new PointD(x,y));
}
这个时间主要浪费在.ToDouble上了。如果能直接按照double类型读取就好了。
你的List会有多长,你真的一次就需要这么多数据么?
还是用一部分读一部分的好。