Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 性能调整powershell文本处理_C#_Performance_Powershell - Fatal编程技术网

C# 性能调整powershell文本处理

C# 性能调整powershell文本处理,c#,performance,powershell,C#,Performance,Powershell,我有一个用C#编写的SSIS脚本任务,我希望将它移植到powershell中用作脚本。C#版本的运行时间为12.1秒,而powershell版本的运行时间为100.5秒,几乎慢了一个数量级。我正在处理11个文本文件(csv),每种格式大约有300-400万行: <TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL> AUDJPY,2

我有一个用C#编写的SSIS脚本任务,我希望将它移植到powershell中用作脚本。C#版本的运行时间为12.1秒,而powershell版本的运行时间为100.5秒,几乎慢了一个数量级。我正在处理11个文本文件(csv),每种格式大约有300-400万行:

<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>
以下是我的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#脚本任务中相同的性能?

除非在powershell中实际使用C,否则无法获得与C#相当的powershell性能。
addtype
cmdlet允许编译一些通常不重要的C代码片段,并直接从脚本调用它们。如果性能是一个问题,并且由于某些原因无法使用C#Assembly,那么我会这样做


参见此处示例:

不久前,我看到一个问题并试图回答它-请看。坦率地说,使用PowerShell时的性能损失是如此巨大,以至于对于耗时的任务,我总是选择C#或@Roman建议的添加类型。

您将C#转换为PowerShell,这在所有情况下可能都不理想。是的,使用C#可以提高性能,但这并不意味着您无法与Powershell相比获得更好的性能

您应该尝试利用Powershell管道中的“流式传输”

例如,类似于:

gc file.txt | ?{ process.....} | %{process...} | out-file out.txt
当对象一可用就沿着管道传递时,速度会更快


您是否可以使用
Get Content
和管道来尝试一个等效方法?

很好奇,您是否打算在两个循环示例中调用ReadLine()三次?看起来它将跳过一行,匹配第二行,打印第三行,然后重复。
gc file.txt | ?{ process.....} | %{process...} | out-file out.txt