From: Raul Parolari Date: 2007-11-27T17:39:05+09:00 Subject: Re: Moving files matching Regex Robert Klemme wrote: >> all_files = Dir.glob("*") >> my_files = all_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x) >> end > > We completely forgot about Dir.entries()... That might be a tad > faster because supposedly no globbing is going on. > The solution with Dir.entries is a bit faster: def get_file_names5 all_files = Dir.entries(".") my_files = all_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x) end # 30 target files present Benchmark.bm(5) do |timer| timer.report('get_file_names2') {10_000.times {get_file_names2} } timer.report('get_file_names5') {10_000.times {get_file_names5} } end user system total real get_file_names2 1.410000 1.260000 2.670000 ( 2.675148) get_file_names5 1.190000 1.260000 2.450000 ( 2.450649) A 10% gain; not much, but the final winner of the marathon is: Nr 5! :-) Raul -- Posted via http://www.ruby-forum.com/.