Ruby Benchmark example script


SUBMITTED BY: Guest

DATE: May 22, 2015, 12:03 p.m.

FORMAT: Ruby

SIZE: 491 Bytes

HITS: 571

  1. require 'benchmark'
  2. n = 12352834
  3. t = 40000
  4. Benchmark.bm do |x|
  5. x.report("Split map") do
  6. t.times { n.to_s.split('').map(&:to_i) }
  7. end
  8. x.report("chars") do
  9. t.times { n.to_s.chars.map(&:to_i) }
  10. end
  11. x.report("each byte") do
  12. t.times { n.to_s.each_byte.map { |x| x - 48 } }
  13. end
  14. x.report("Numerical") do
  15. t.times { (0..Math.log10(n).to_i).map { |dp| n / 10 ** dp % 10 }.reverse }
  16. end
  17. end

comments powered by Disqus