powershell可以更快地将大数据读取和写入文本文件

powershell可以更快地将大数据读取和写入文本文件,powershell,Powershell,首先程序读取大约80000行数据,拆分行 拆分对象,搜索文件,然后拆分对象 搜索文件使用:$Sel=选择字符串-模式$point\u ID-路径$Location 写入文件:$SDID_XDIS | out file-filepath$fname_mu-Append 完成写文本文件大约1小时,速度非常慢 我该怎么办?任何其他更快的完成程序使用Measure命令查找花费时间最多的语句 话虽如此,代码包含两个明显的瓶颈。使用Select String读取大文件的性能会很差。请考虑使用 RealLoa

首先程序读取大约80000行数据,拆分行

拆分对象,搜索文件,然后拆分对象

搜索文件使用:$Sel=选择字符串-模式$point\u ID-路径$Location

写入文件:$SDID_XDIS | out file-filepath$fname_mu-Append

完成写文本文件大约1小时,速度非常慢


我该怎么办?任何其他更快的完成程序

使用
Measure命令
查找花费时间最多的语句

话虽如此,代码包含两个明显的瓶颈。使用
Select String
读取大文件的性能会很差。请考虑使用<代码> RealLoad()/<代码>从代码>系统.IO。文件< /代码>。使用
添加内容
逐行写入文件是另一个性能杀手。相反,可以使用缓冲输出,例如,来自
System.Text
StringBuilder

$SDID\u XDIS的解析例程可能需要重写。考虑多次检查相同的条件:

$m_point = Select-String -pattern  ",T," c:\temp\test\mt_point.txt
For ($x=0; $x -le $m_point.length; $x++)
#For ($x=0; $x -le 2; $x++)

{
    $s_point = $m_point[$x] -split ","
   $s_point = $s_point -replace ''''
  # echo $s_point[10]
   $point_ID=$s_point[2]+"."+$s_point[3]+"."+$s_point[4]+"."+$s_point[5]
   # echo $point_ID
        $Sel = Select-String  -pattern $point_ID -path $Location 
        $tp_point_stirng= $Sel-split ";"
      # echo $tp_point_stirng[6]
       # echo $tp_point_stirng[7]
        $tp_point_no=$tp_point_stirng[0]-split ":"
        $tp_point=$tp_point_no[3]-split ","
        #echo $tp_point[1]
   if($point_ID  -eq $tp_point_stirng[1])
   {
    if($s_point[6]-eq "T")
    {
        $Nis_point="!1!"+$tp_point[1]+"!"+$s_point[2]+ "!"+$tp_point_stirng[7]+"!0!SI!!"+$s_point[7]+"!!!!"+ $s_point[8]+"!"
        $Nis_point | out-file -filepath $fname_tag -Append 

    }
     if($s_point[9]-eq "T")
    {
        $Nis_point="!1!"+$tp_point[1]+"!"+$s_point[2]+ "!"+$tp_point_stirng[7]+"!2!AI!!"+$s_point[10]+"!!!!"+ $s_point[11]+"!"
        $Nis_point | out-file -filepath $fname_tag -Append 
    }
    if($s_point[12] -eq "T" -or $s_point[13] -eq "T")
    {
        if($s_point[14] -eq "F" -or $s_point[15] -eq "F")
        {
           $SDID_XDIS= "1"+","+$tp_point[1]+",0"
           $SDID_XDIS | out-file -filepath $fname_mu -Append 
        }
        if($s_point[14] -eq "T" -or $s_point[15] -eq "F")
        {
           $SDID_XDIS="1"+","+$tp_point[1]+",1"
           $SDID_XDIS | out-file -filepath $fname_mu -Append 
        }
        if($s_point[14] -eq "F" -or $s_point[15] -eq "F")
        {
           $SDID_XDIS= "1"+","+$tp_point[1]+",2"
           $SDID_XDIS | out-file -filepath $fname_mu -Append 
        }
        if($s_point[14] -eq "T" -or $s_point[15] -eq "T")
        {
           $SDID_XDIS= "1"+","+$tp_point[1]+",3"
           $SDID_XDIS | out-file -filepath $fname_mu -Append 
        }
    } 
    }
}

我有一个搜索文本文件的功能。不是很完美,但是[system.io.file]::ReadLines($fullPath)对于大文件要快得多。因为你可以试试System.IO.StreamWriter($fullPath)。我看到很多字符串连接在那里(非常慢)。如果$m_results是一个大型集合,则内存管理可能会降低您的速度,这取决于您从文件中获取的匹配记录的数量。切换到获取-ReadCount约为1000的内容,然后使用foreach在管道中对生成的数组使用-Match可能会更快。
    if($s_point[14] -eq "F" -or $s_point[15] -eq "F") # Match F_ or _F
    {
       ...
    }
    if($s_point[14] -eq "T" -or $s_point[15] -eq "F") # Match T_ or _F
    {
       ... # Huh? _F was already checked in the previous step?
    }
    if($s_point[14] -eq "F" -or $s_point[15] -eq "F") # Match F_ or _F
    {
       ... # Huh? Didn't we check this already?           
    }
    if($s_point[14] -eq "T" -or $s_point[15] -eq "T") # Match T_ or _T
    {
       ...
    }