From: dblack@... Date: 2003-05-04T02:18:44+09:00 Subject: Re: Cross platform `ls -t` Hi -- On Sun, 4 May 2003, Hal E. Fulton wrote: > ----- Original Message ----- > From: "gabriele renzi" > Newsgroups: comp.lang.ruby > To: "ruby-talk ML" > Sent: Saturday, May 03, 2003 11:31 AM > Subject: Cross platform `ls -t` > > > > Is there a way using Dir to have a list of directory entries sorted by > > time? > > > > What I'd like to have is a `ls -t` that works even on windows > > (and no, I don't want ls.exe ;) > > Mmm. Something like this (untested): > > list = Dir["**"].sort {|x,y| File.stat(x).mtime < File.stat(y).mtime } > > Of course, it's inefficient -- a Schwartzian transform > would make it faster. Procrastinating on real work, I tried it out: require 'benchmark' include Benchmark n = 1000 bm do |b| b.report("Non-Schwartzian:\n") do n.times { Dir["**"].sort {|x,y| # (Put y test first to duplicate order of ls -t) File.stat(y).mtime <=> File.stat(x).mtime } } end b.report("Schwartzian:\n") do n.times { Dir["**"].map {|f| [File.stat(f).mtime,f] }.sort {|x,y| y[0] <=> x[0] }.map{|a| a[1] } } end Output: user system total real Non-Schwartzian: 7.560000 2.260000 9.820000 ( 9.827662) Schwartzian: 4.150000 0.260000 4.410000 ( 4.422282) Cool :-) David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav