From: Robert Klemme Date: 2007-11-26T21:35:41+09:00 Subject: Re: Moving files matching Regex 2007/11/26, Mark Woodward : > On Sun, 25 Nov 2007 20:59:46 +1100 > Mark Woodward wrote: > > thanks to all replies. Very informative. > I've benchmarked this and 'get_file_names2' is considerably quicker! > (if I'm benchmarking correctly?) > > Could someone explain why? ie what is it about get_file_names2 that > makes it so much quicker? What is it about get_file_names that slows it > down? In _2 you do a dir glob in *one* directory only while find recursively descends a *directory tree*. Even though you prune in some cases it comes as no surprise that this is slower. > def get_file_names > fn=[] > Find.find('./') do |path| > Find.prune if File.basename(path) == 'sent' > curr_file = File.basename(path) > if curr_file =~ FILE_REGEX > fn << curr_file > end > end > fn > end > > > def get_file_names2 > fglob= %r{^(CAL|NCPH|GOH)\d{6}\.xls$}x > all_files = Dir.glob("*") > my_files = all_files.grep(fglob) > end > > > > Benchmark.bm(5) do |timer| > timer.report('get_file_names') {10000.times {get_file_names}} > timer.report('get_file_names2') {10000.times {get_file_names2}} > end > > > Results: > > user system total real > get_file_names 3.040000 0.700000 3.740000 (5.440394) > get_file_names2 0.570000 0.160000 0.730000 (0.997355) > Kind regards robert -- use.inject do |as, often| as.you_can - without end