LeonMMS

Untitled

Oct 18th, 2020
809
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'benchmark'
  2. require 'benchmark/memory'
  3. #meu teste
  4. def mydelete(a)
  5.   i = 0
  6.   while(i < a.size)
  7.     if a[i] == 2
  8.       a.delete_at(i)
  9.     else
  10.       i += 1
  11.     end
  12.   end
  13.   return a
  14. end
  15. #teste do val
  16. def valdelete(a)
  17.   ary2 = []
  18.   a.each_with_index do |e, i|
  19.     if e == 2
  20.       ary2 << i
  21.     end
  22.   end
  23.   ary2.reverse_each do |i|
  24.     a.delete_at(i)
  25.   end
  26.   return a
  27. end
  28. # benchmark setup
  29. def benchmark(a)
  30.   puts "Benchmark.bmbm"
  31.   Benchmark.bmbm do |x|
  32.     x.report("My delete") {mydelete(a)}
  33.     x.report("Val delete") {valdelete(a)}
  34.   end
  35.   puts "Benchmark.memory"
  36.   Benchmark.memory do |x|
  37.     x.report("My delete") {mydelete(a)}
  38.     x.report("Val delete") {valdelete(a)}
  39.   end
  40. end
  41. benchmark( [2, 5, 2, 7, 2, 9])
RAW Paste Data