PowerShell is slow (much slower than Python) in large Search/Replace operation?

前端 未结 5 1274
长情又很酷
长情又很酷 2021-02-02 12:41

I have 265 CSV files with over 4 million total records (lines), and need to do a search and replace in all the CSV files. I have a snippet of my PowerShell code below that does

5条回答
  •  轮回少年
    2021-02-02 13:00

    Actually, I'm faced with a similar issue right now. With my new job, i have to parse huge text files to pull information based on certain criteria. The powershell script (optimized to the brim) takes 4 hours to return a fully processed csv file. We wrote another python script that took just under 1 hour...

    As much as i love powershell, i was heart broken. For your amusement, try this: Powershell:

    $num = 0
    $string = "Mary had a little lamb"
    
    while($num -lt 1000000){
        $string = $string.ToUpper()
        $string = $string.ToLower()
        Write-Host $string
        $num++
    }
    

    Python:

    num = 0
    string = "Mary had a little lamb"
    
    while num < 1000000:
        string = string.lower()
        string = string.upper()
        print(string)
        num+=1
    

    and trigger the two jobs. You can even encapsulate in Measure-command{} to keep it "scientific".

    Also, link, crazy read..

提交回复
热议问题