From: Robert Klemme Date: 2008-01-15T00:37:19+09:00 Subject: Re: Counting the files in a directory.... 2008/1/14, Siep Korteling : > Robert Klemme wrote: > > 2008/1/12, Siep Korteling : > >> > puts folder.Files.count > > Did you verify that this is faster? I am skeptical because this call > > does basically the same: it gets the list of files in the directory > > and counts them. I would expect a speedup only if there was an API > > function that would directly return the number of files. > > > > Kind regards > > > > robert > > require 'win32ole' > require 'benchmark' > ldirname = "C:/WINDOWS/system32" #2500+ files > mdirname = "C:/ruby/lib/ruby/1.8" #800+ files > sdirname="C:/ruby/lib/ruby/1.8/i386-mswin32" # 40+ files > @fso = WIN32OLE.new("Scripting.FileSystemObject") > n=500 > > Benchmark.bmbm do |x| > > x.report("fso_mixed > |"){n.times{fso(ldirname);fso(mdirname);fso(sdirname)}} > x.report("Dir_mixed > |"){n.times{dir(ldirname);dir(mdirname);dir(sdirname)}} > x.report("fso_2500 |"){n.times{fso(ldirname)}} > x.report("Dir_2500 |"){n.times{dir(ldirname)}} > x.report("fso_800 |"){n.times{fso(mdirname)}} > x.report("Dir_800 |"){n.times{dir(mdirname)}} > x.report("fso_40 |"){n.times{fso(sdirname)}} > x.report("Dir_40 |"){n.times{dir(sdirname)}} > > def fso(dirname) > folder = @fso.GetFolder(dirname) > count = folder.Files.count > end > > def dir(dirname) > count = Dir.entries(dirname).size - 2 > end > > end > > > results in: > > user system total real > fso_mixed | 0.360000 1.222000 1.582000 ( 1.673000) > Dir_mixed | 3.635000 1.382000 5.017000 ( 5.157000) > fso_2500 | 0.271000 1.071000 1.342000 ( 1.362000) > Dir_2500 | 3.305000 1.282000 4.587000 ( 4.697000) > fso_800 | 0.040000 0.120000 0.160000 ( 0.160000) > Dir_800 | 0.170000 0.100000 0.270000 ( 0.281000) > fso_40 | 0.020000 0.080000 0.100000 ( 0.110000) > Dir_40 | 0.050000 0.071000 0.121000 ( 0.120000) > > Apparently, for small directories it doesn't make much difference. For > large directories it does. (filesystem ntfs). Amazing. So there is probably some room for improvement of the Windows build of Ruby. :-) Btw, you did not do the subtraction of two - does GetFolder not return "." and ".."? Kind regards robert -- use.inject do |as, often| as.you_can - without end