On 12/9/05, Eric Hodel <drbrain / segment7.net> wrote: > On Dec 9, 2005, at 11:23 AM, pat eyler wrote: > > > > > seen_ary = Array.new > > > > File.open("nums","r").each do |elem| > > print elem if seen_ary.include?(elem) > > seen_ary.push(elem) > > end > > > > (there are probably still better ways of doing this though) > > I'll go with: > > seen = {} > > ARGF.each do |elem| > print elem if seen.include? elem > seen[elem] = true > end > And yes, this way is much better (I knew it would be but, it's nice to see just how much better): require 'benchmark' n = 100 # build a big array with lots of duplication @nums = (1..1_000).to_a (1..1_000).step(17) {|num| @nums.push(num) } # set up two versions of our rubylike version # the original perl-like version and the # final version def as_ary seen_ary = Array.new @nums.each do |elem| $stderr.print elem if seen_ary.include?(elem) seen_ary.push(elem) end end def as_hash seen = Hash.new @nums.each do |elem| $stderr.print elem if seen.include?(elem) seen[elem] = true end end # then benchmark them to see how they perform Benchmark.bm(10) do |x| x.report("as Array") {for i in 1..n; as_ary; end } x.report("as Hash") {for i in 1..n; as_hash; end } end # and the results are: # ./print_dups.rb 2> /dev/null # user system total real #as Array 12.690000 0.030000 12.720000 ( 13.940651) #as Hash 0.230000 0.000000 0.230000 ( 0.613217) > -- > Eric Hodel - drbrain / segment7.net - http://segment7.net > This implementation is HODEL-HASH-9600 compliant > > http://trackmap.robotcoop.com > > > > -- thanks, -pate -------------------------