简体   繁体   English

Powershell 进口csv内慢搜

[英]Powershell slow searching within imported csv

I am using a csv file to load a table-like object and then search this for existing items.我正在使用 csv 文件来加载类似表格的 object,然后在其中搜索现有项目。 With only 2000 items, a computer running on a I5 CPU takes 4 seconds perform search using where on 2 columns.只有 2000 个项目,运行在 I5 CPU 上的计算机需要 4 秒使用where on 2 列执行搜索。 Wondering what I am doing wrong:想知道我做错了什么:

$uploadedRecordings = import-csv -path $ArchiveUploadedFilesInGoogleDrive
...[some other code]
if($uploadedRecordings | where { $_.Name -eq $filename -and $_.Size -eq $item.file_size}){
               Write-Host "[Already downloaded] Skipping....
}

Where $item (sample):其中$item (样本):

id              : f5b693
meeting_id      : uT4dfhghd==
recording_start : 2020-03-25T16:01:31Z
recording_end   : 2020-03-25T18:14:36Z
file_type       : M4A
file_size       : 54332420
play_url        : https://myurl
download_url    : https://otherurl
status          : completed
recording_type  : audio_only

and $filename = "Meeting - 2020-04-20 -- 09.29.59.mp4"$filename = "Meeting - 2020-04-20 -- 09.29.59.mp4"

PowerShell offers great features, far beyond what traditional shells offer, but one thing it is not: a speed demon. PowerShell 提供了强大的功能,远远超出了传统 shell 提供的功能,但它不是:速度恶魔。

PowerShell's object-oriented pipeline is a wonderful tool, but it can be slow. PowerShell 的面向对象管道是一个很棒的工具,但它可能很慢。

This answer summarizes performance recommendations;这个答案总结了性能建议; in the case at hand, you can speed up your command by avoiding the pipeline in favor of the .Where() array method :在手头的情况下,您可以通过避免使用.Where()数组方法来加快命令速度:

if ($uploadedRecordings.Where({ 
  $_.Size -eq $item.file_size -and $_.Name -eq $filename 
})) {
  Write-Host "[Already downloaded] Skipping..."
}

Also note how I've swapped the -and operands in favor of comparing file sizes first, to take advantage of short-circuiting;还要注意我如何交换-and操作数以支持首先比较文件大小,以利用短路; after all, files being exactly identical in size is less common than their having the same name.毕竟,大小完全相同的文件不如同名文件常见。

You may be able to speed things up further a bit by caching $item.file_size in an auxiliary variable, though my hunch is that that won't make much of a difference in practice.可以通过在辅助变量中缓存$item.file_size来进一步加快速度,尽管我的直觉是这在实践中不会有太大的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM