=================Perl:查找大于X分钟数的文件的最快方法,将最早排序到最新?
1.查找文件不是几分钟的X数的
2.处理他们从最旧到最新
下面的代码工作正常,但是该目录包含3百万个文件。因此我需要优化它以更快地找到文件。我不必担心文件的内容只是名称。
###########################
sub get_files_to_process{
###########################
# Declare arrays
my @xmlfiles;
my @qulfiedfiles;
# Declare a Dictionary
my %filedisc;
opendir(my $dh, $maindir) or die "opendir($maindir): $!";
# Read all the files
while (my $de = readdir($dh)) {
# get the Full path of the file
my $f = $maindir . $de;
# If File is there and has .xml Extension
if (-f $f && $f=~ /\.xml/){
# Put it in a XMLFILES Array
push (@xmlfiles, $f); }
}
closedir($dh);
# For every file in directory
for my $file (@xmlfiles) {
# Get stats about a file
my @stats = stat($file);
# If time stamp is older than minutes provided
if ($stats[9] <= ($now - (($minutesold * 60)))){
# Put the File and Time stamp in the dictionary
$filedisc{$file} = $stats[9];
}
}
# For every file in the dictionary sort based on the timestamp oldest files first
for my $x (sort {$filedisc{$a} <=> $filedisc{$b} or $a cmp $b } keys %filedisc) {
# Put the qualified files (Based on the age) in a list
push(@qulfiedfiles, $x);}
更新:到目前为止,这似乎是做有前途的,更多的测试:
##########################
sub get_files_count{
##########################
my $cmd= "find $maindir -maxdepth 1 -name '*.xml' -mmin +$minutesold -printf \"%T+\t%p\\n\"| sort";
my @output = `$cmd`;
if (@output){
foreach my $line (@output){
chomp $line;
push (@files2process, (split '\t', $line)[ -1 ]);
}
}
}
我还没有测试您的代码与3_000_000条目的目录。在我们开始讨论优化代码的方法之前,为什么不使用'Devel :: NYTProf'来确定代码花费最多的时间? – DavidO 2014-08-27 21:21:04
一旦你有你的名单,你如何处理文件? – ThisSuitIsBlackNot 2014-08-27 21:35:42
@ThisSuitIsBlackNot,一旦我有文件列表,我根据文件的名称将文件移动到适当的子目录(并创建子目录(如果尚未存在的话)),则此脚本将每隔cron用完5到10分钟,Enterpise应用程序会生成大量的xml文件,这将会培养这些文件 – Grene 2014-08-28 01:30:01