dblack / superlink.net wrote:
...
> Procrastinating on real work, I tried it out:

Most of the best work is done that way...

> 
>   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

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)