我有一个C#程序,它正在提取一个大约的.csv文件.42,000行长.文件中的所有数据都存储如下:
Zipcode,City,State
我将所有信息都分成三个不同的列listview
.
目前这个数据大约需要30到50秒才能进入我的程序.我的问题是如何才能更好地优化我的代码以缩短时间?
以下是我的代码片段.评论的代码是我之前尝试的代码,但没有成功减少时间,因此我以一种更容易阅读的方式重写了它.
//These are globally declared.
lvZip.Columns.Add("Zipcode", 150, HorizontalAlignment.Left);
lvZip.Columns.Add("City", 150, HorizontalAlignment.Left);
lvZip.Columns.Add("State", 150, HorizontalAlignment.Left);
lvZip.View = View.Details;
lvZip.Items.Clear();
//string dir = System.IO.Path.GetDirectoryName(
// System.Reflection.Assembly.GetExecutingAssembly().Location);
//string path = dir + @"\zip_code_database_edited.csv";
//var open = new StreamReader(File.OpenRead(path));
//foreach (String s in File.ReadAllLines(path))
//{
// Zipinfo = s.Split(',');
// Zipinfo[0] = Zipinfo[0].Trim();
// Zipinfo[1] = Zipinfo[1].Trim();
// Zipinfo[2] = Zipinfo[2].Trim();
// lvItem = new ListViewItem(Zipinfo);
// lvZip.Items.Add(lvItem);
//}
//open.Close();
StreamReader myreader = File.OpenText(path);
aLine = myreader.ReadLine();
while …
Run Code Online (Sandbox Code Playgroud) 我试图使用Python读取二进制文件.其他人使用以下代码使用R读取数据:
x <- readBin(webpage, numeric(), n=6e8, size = 4, endian = "little")
myPoints <- data.frame("tmax" = x[1:(length(x)/4)],
"nmax" = x[(length(x)/4 + 1):(2*(length(x)/4))],
"tmin" = x[(2*length(x)/4 + 1):(3*(length(x)/4))],
"nmin" = x[(3*length(x)/4 + 1):(length(x))])
Run Code Online (Sandbox Code Playgroud)
使用Python,我正在尝试以下代码:
import struct
with open('file','rb') as f:
val = f.read(16)
while val != '':
print(struct.unpack('4f', val))
val = f.read(16)
Run Code Online (Sandbox Code Playgroud)
我的结果略有不同.例如,R中的第一行返回4列为-999.9,0,-999.0,0.对于所有四列(下图),Python返回-999.0.
我知道他们正在使用一些[]
代码来填充文件的长度,但是我不知道在Python中究竟是如何做到这一点的,我也不明白为什么他们这样做.基本上,我想重新创建R在Python中所做的事情.
如果需要,我可以提供更多代码库.我不想用不必要的代码来压倒.
背景:
使用以下Fortran代码在Linux机器上读取二进制文件:
parameter(nx=720, ny=360, nday=365)
c
dimension tmax(nx,ny,nday),nmax(nx,ny,nday)
dimension tmin(nx,ny,nday),nmin(nx,ny,nday)
c
open(10,
&file='FILE',
&access='direct',recl=nx*ny*4)
c
do k=1,nday
read(10,rec=(k-1)*4+1)((tmax(i,j,k),i=1,nx),j=1,ny)
read(10,rec=(k-1)*4+2)((nmax(i,j,k),i=1,nx),j=1,ny)
read(10,rec=(k-1)*4+3)((tmin(i,j,k),i=1,nx),j=1,ny)
read(10,rec=(k-1)*4+4)((nmin(i,j,k),i=1,nx),j=1,ny)
end do
Run Code Online (Sandbox Code Playgroud)
档案细节:
options little_endian
title global daily analysis (grid box mean, the grid shown is the center of the grid box)
undef -999.0
xdef 720 linear 0.25 0.50
ydef 360 linear -89.75 0.50
zdef 1 linear 1 1
tdef 365 linear 01jan2015 1dy
vars 4
tmax 1 00 daily maximum temperature (C)
nmax 1 00 number of reports …
Run Code Online (Sandbox Code Playgroud) binaryfiles ×2
python ×2
c# ×1
fortran ×1
io ×1
listview ×1
r ×1
streamreader ×1
streamwriter ×1