我有一个工作的perl脚本扫描目录并使用imgsize http://dktools.sourceforge.net/imgsize.html来获取png文件的宽度等.有没有人有任何关于加快这个过程的技巧(现在,平均每1000个文件需要5分钟)?我只是想知道代码是否可以如何优化.谢谢.
use strict;
use warnings;
use File::Find;
my @files;
my $directory = '/Graphics/';
my $output_file = '/output_file';
my $max_height = 555;
my $count = 0;
open ( OUTPUT, '>>', $output_file );
find( \&wanted, $directory );
foreach my $file ( @files ) {
if ( $file =~ /\.png$/ ) {
my $height = `imgsize $file | cut -d\'\"\' -f4`;
if ( $height > $max_height ) {
print OUTPUT "$file\n";
}
$count++;
my $int_check = $count/1000;
if ( $int_check !~ /\D/ ) {
print "processed: $count\n";
}
}
}
print "total: $count\n";
close ( OUTPUT );
exit;
sub wanted {
push @files, $File::Find::name;
return;
}
Run Code Online (Sandbox Code Playgroud)
解决方案:事实证明我能够使用该Image::Info模块.我从每5分钟处理1000个imgs到每12秒处理一次.以下是相关的代码片段,如果有人有兴趣:
use Image::Info qw(image_info);
foreach my $file ( @files ) {
if ( $file =~ /\.png$/ ) {
my $output = image_info($file);
my $height = ${$output}{height};
if ($height > $max_height) {
print OUTPUT "$file\n";
}
$count++;
my $int_check = $count/1000;
if ( $int_check !~ /\D/ ) {
print "processed: $count\n";
}
}
}
Run Code Online (Sandbox Code Playgroud)
您展示的Perl代码可能不是罪魁祸首.你可以用Devel :: NYTProf来描述它,就像@choroba所说的那样.但我敢打赌,大部分时间来自每个图像分支两个外部进程(imgsize和cut).您应该查看可以在不运行任何外部进程的情况下检索图像高度的Perl模块.想到像Image :: Info这样的模块.
| 归档时间: |
|
| 查看次数: |
420 次 |
| 最近记录: |