Skip to content

Instantly share code, notes, and snippets.

@szajbus
Created November 25, 2009 16:54
Show Gist options
  • Select an option

  • Save szajbus/242870 to your computer and use it in GitHub Desktop.

Select an option

Save szajbus/242870 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'benchmark'
include Benchmark
pi = 3.14
n = 1000000
Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR) do |x|
to_i = x.report("to_i:") { n.times { pi != pi.to_i } }
gsub = x.report("gsub:") { n.times { pi.to_s.gsub(/^.*\.(.*)$/, '\1').to_i > 0 } }
mod = x.report("mod:") { n.times { pi % 1 != 0 } }
fc = x.report("fc:") { n.times { pi.floor != pi.ceil } }
end
user system total real
to_i: 0.410000 0.000000 0.410000 ( 0.410188)
gsub: 6.050000 0.020000 6.070000 ( 6.093139)
mod: 0.530000 0.000000 0.530000 ( 0.529583)
fc: 0.490000 0.000000 0.490000 ( 0.494660)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment