If you want to minimize memory usage, you will have to suffer in speed somewhere, somehow. There is a trade off that you have to make.
I think one of the easiest method is to use a proper database to record your files, like MySQL, Postgres, Oracle, etc. Create a table with an *index* on the filename to speed up SQL query. Let the database do the search optimization for you.
#!/usr/local/bin/perl -w
use strict;
use DBI;
use DBD::Sybase;
use File::Find;
my $dbh = ....; # connect to database
my $sth = $dbh->prepare("select filename from bench where filename=?")
+;
# look for new files
my @new_files;
find( { follow => 1, no_chdir => 1,
wanted =>
sub {
if (! /\.$/) { # ignore unwanted . or ..
$sth->execute($_);
my $file_exists;
while (my @res = $sth->fetchrow_array()) { $file_exists+
++ }
push @new_files if !$file_exits;
}
}
}, '/');
$sth->finish;
# insert new files into the database
$sth = $dbh->prepare("insert into bench (filename) values (?)");
$sth->execute($_) for @new_files;
$sth->finish;
# do stuff with @new_files ....
I can think of another solution, although the performance in speed is not great, that uses resonable amount of memory.
#!/usr/local/bin/perl -w
use strict;
use File::Find;
my $bench_file = 'bench.txt';
my @new_files;
find( { follow => 1, no_chdir => 1,
wanted => \&callback }, '/');
sub callback {
if (! /\.$/) { # ignore unwanted . or ..
if ( ! `grep '$_' $bench_file` ) {
push @new_files, $_; # remember this file
}
}
}
# append unseen filenames to bench.txt file
open BENCH, ">>bench.txt"
or die "Can not append to bench.txt";
print BENCH "$_\n" foreach (@new_files);
close BENCH;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|