Skip to content

Instantly share code, notes, and snippets.

@youroff
Last active February 8, 2018 02:55
Show Gist options
  • Save youroff/dc4f0ff71083acf6c180ec33f3c52881 to your computer and use it in GitHub Desktop.
Save youroff/dc4f0ff71083acf6c180ec33f3c52881 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Sqr
def initialize(xs)
@xs = xs
end
def run
puts "INLINE"
puts Benchmark.measure { @xs.map { |x| x * x } }
puts "INSTANCE"
puts Benchmark.measure { @xs.map { |x| sqr(x) } }
puts "CLASS"
puts Benchmark.measure { @xs.map { |x| self.class.sqr(x) } }
end
def sqr(x)
x * x
end
def self.sqr(x)
x * x
end
end
Sqr.new(0..100000000).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment