我写了scraper来下载约3200种股票的10年股票数据。每个库存都会下载到自己的文本文件中,该文件看起来像this。
现在我正在尝试使用我下载的数据填充本地数据库。它确实有效,但速度极慢。我一次添加一个记录。因此,如果一个股票有2500个数据点,那么我将AddRow()调用2500次。
我从参数开始,现在我正在使用实体框架。两者都很慢。为了给你一个想法,这是我用来填充数据库的函数:
private DateTime ParseDate(string date) {
string[] parts = date.Split('-');
int year = Int16.Parse(parts[0]);
int month = Int16.Parse(parts[1]);
int day = Int16.Parse(parts[2]);
return new DateTime(year, month, day);
}
public void PopulateFromCSVs(IStockHistory repository)
{
string symbolsPath = scraperDir + @"\symbols\symbols.txt";
string[] symbols = File.ReadAllText(symbolsPath).Split(',');
int count = symbols.Length;
for (int i = 0; i < count; i++)
{
string line;
int curLineNum = 0;
string curFile = scraperDir + @"\history\" + symbols[i] + ".txt";
if (!File.Exists(curFile))
continue;
using (StreamReader file = new StreamReader(curFile))
{
while ((line = file.ReadLine()) != null)
{
curLineNum++;
string[] row = line.Split(',');
if (curLineNum > 1)
{
repository.AddStockRecord(new StockRecord()
{
Symbol = symbols[i],
RecordDate = ParseDate(row[0]),
OpenValue = float.Parse(row[1]),
HighValue = float.Parse(row[2]),
LowValue = float.Parse(row[3]),
CloseValue = float.Parse(row[4]),
Volume = float.Parse(row[5]),
AdjustedCloseValue = float.Parse(row[6])
});
}
}
}
}
}
它可能会更好,但它不是疯了。也就是说,没有任何代码可以花费很长时间没有任何理由。为了测试该功能,我只填充了5种股票(175,000行)的数据。它确实向数据库添加了数据,但速度非常慢。
作为比较它的东西,下载和连接数据的脚本可以在几秒钟内读取每个文件中的每个记录。
[编辑]抱歉,它更像是17,500,而不是175,000。
答案 0 :(得分:1)