From: Mark Woodward Date: 2007-11-26T19:45:06+09:00 Subject: Re: Moving files matching Regex 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? 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) cheers, -- Mark