On Wed, 26 Jul 2006, Kroeger, Simon (ext) wrote:

> Just to show the beauty of ruby:
> -----------------------------------------------------------
> require 'rubygems'
> require 'permutation'
> require 'set'
>
> $size = (ARGV.shift || 5).to_i
>
> $perms = Permutation.new($size).map{|p| p.value}
> $out = $perms.map{|p| p.map{|v| v+1}.join}
> $filter = $perms.map do |p|
>  s = SortedSet.new
>  $perms.each_with_index do |o, i|
>    o.each_with_index {|v, j| s.add(i) if p[j] == v}
>  end && s.to_a
> end
>
> $latins = []
> def search lines, possibs
>  return $latins << lines if lines.size == $size
>  possibs.each do |p|
>    search lines + [p], (possibs -
> $filter[p]).subtract(lines.last.to_i..p)
>  end
> end
>
> search [], SortedSet[*(0...$perms.size)]
>
> $latins.each do |latin|
>  $perms.each do |perm|
>    perm.each{|p| puts $out[latin[p]]}
>    puts
>  end
> end
> -----------------------------------------------------------
> (does someone has a nicer/even faster version?)
>
> would you please run that on your machine?
> perhaps you have to do a "gem install permutation"
> (no I don't think it's faster than your C code, but
> it should beat the perl version)
>
>> If you really really want that performance boost then take
>> the following
>> advice very seriously - "Write it in C".
>
> Agreed, 100%, for those who want speed, speed and nothing
> else there is hardly a better way.
>
> thanks
>
> Simon

harp:~ > time ruby latin.rb 5 > 5.out
real    0m11.170s
user    0m10.840s
sys     0m0.040s

harp:~ > uname -srm
Linux 2.4.21-40.EL i686

harp:~ > cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Pentium(R) 4 CPU 2.40GHz
stepping        : 7
cpu MHz         : 2386.575
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips        : 4757.91

harp:~ > ruby -v
ruby 1.8.4 (2005-12-01) [i686-linux]


not too shabby.  definite not worth the hassle for 5 seconds of c.

-a
-- 
suffering increases your inner strength.  also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama