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
thanks in advance
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
thanks in advance
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.
Hal
----- Original Message -----
From: “gabriele renzi” surrender_it@rc1.vip.lng.yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, May 03, 2003 11:31 AM
Subject: Cross platform ls -t
thanks everybody for the answers, I suppose the sort_by solution will
be what I’ll use.
Still, I don’t get why you’re doing
Dir["**"]
instead of
Dir["*"]
someone would explain me this ?
il Sat, 03 May 2003 16:11:07 GMT, gabriele renzi surrender_it@remove.yahoo.it ha scritto::
Hi –
From: “gabriele renzi” surrender_it@rc1.vip.lng.yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, May 03, 2003 11:31 AM
Subject: Cross platformls -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.exeMmm. 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
On Sun, 4 May 2003, Hal E. Fulton wrote:
----- Original Message -----
–
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
http://www.rubycentral.com/book/ref_c_dir.html
** Matches subdirectories recursively
Regards,
Brian.
On Sun, May 04, 2003 at 09:33:18AM +0900, gabriele renzi wrote:
Still, I don’t get why you’re doing
Dir[“**”]
instead of
Dir[“*”]someone would explain me this ?
Procrastinating on real work, I tried it out:
That’s the spirit!
[snip]
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
Cool indeed.
You neglected to say whether the list was correct,
but I assume it was, or you’d have complained.
As for the timings: I was formulating an argument
that the ratio could be represented as T(N)/N**2
where T(N) is the triangular number 1+2+3+…+N
or better T(N)=N*(N+1)/2.
But my formulating was giving me a headache, so
I quit.
Hal
----- Original Message -----
From: dblack@superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, May 03, 2003 12:18 PM
Subject: Re: Cross platform ls -t
dblack@superlink.net wrote:
…
Procrastinating on real work, I tried it out:
Most of the best work is done that way…
require ‘benchmark’
include Benchmarkn = 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
Internally, sort_by also uses the Schwartzian algorithm, but I guess
it’s faster because more of it is in C:
b.report("Schwartzian using sort_by:\n") do
n.times {
Dir["**"].sort_by {|x|
File.stat(x).mtime
}
}
end
end
user system total real
Non-Schwartzian:
2.730000 1.210000 3.940000 ( 4.226551)
Schwartzian:
1.080000 0.570000 1.650000 ( 1.747052)
Schwartzian using sort_by:
0.690000 0.480000 1.170000 ( 1.246813)