【发布时间】:2011-08-03 11:04:31
【问题描述】:
我有一个用 C# 编写的 SSIS 脚本任务,我希望将它移植到 powershell 以用作脚本。 C# 版本运行时间为 12.1s,但 powershell 版本需要 100.5s,几乎慢了一个数量级。我正在处理 11 个文本文件 (csv),每种格式大约有 3-4 百万行:
<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>
AUDJPY,20010102,230100,64.30,64.30,64.30,64.30,4
AUDJPY,20010102,230300,64.29,64.29,64.29,64.29,4
<snip>
我只想将内容写入一个新文件,其中列的日期为 20110101 或更晚。这是我的 C# 版本:
private void ProcessFile(string fileName)
{
string outfile = fileName + ".processed";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(fileName))
{
string line;
int year;
while ((line = sr.ReadLine()) != null)
{
year = Convert.ToInt32( sr.ReadLine().Substring(7, 4));
if (year >= 2011)
{
sb.AppendLine(sr.ReadLine());
}
}
}
using (StreamWriter sw = new StreamWriter(outfile))
{
sw.Write(sb.ToString());
}
}
这是我的 powershell 版本:
foreach($file in ls $PriceFolder\*.txt) {
$outFile = $file.FullName + ".processed"
$sr = New-Object System.IO.StreamReader($file)
$sw = New-Object System.IO.StreamWriter($outFile)
while(($line = $sr.ReadLine() -ne $null))
{
if ($sr.ReadLine().SubString(7,4) -eq "2011") {$sw.WriteLine($sr.ReadLine())}
}
}
如何在 Powershell 中获得与在 SSIS 中的 C# 脚本任务中获得相同的性能?
【问题讨论】:
-
只是好奇,你打算在两个循环示例中调用 ReadLine() 三次吗?看起来它会跳过一行,匹配第二行,打印第三行,然后重复。
标签: c# performance powershell