Skip to content

Instantly share code, notes, and snippets.

@gmarik
Last active June 22, 2020 03:24
Show Gist options
  • Save gmarik/792658 to your computer and use it in GitHub Desktop.
Save gmarik/792658 to your computer and use it in GitHub Desktop.
stuff
class Fixnum
def upto2(n)
(self..n).to_a.map{|k| yield k }
end
end
class A
def sum(n)
sum = 0
1.upto(n) do |i|
sum += sql(n)
sum += sql(n-1)
end
end
def sql(n)
Kernel.rand(n)
end
end
class B; def he; p 'hello'; end; end
require 'singleton'
class Benchmark
include Singleton
def for(klass, method = klass.instance_methods(false))
overriden_methods = (Array(method) + ['initialize']).map do |m|
<<-EOR
unless method_defined?(:_benchmark_#{m})
alias_method :_benchmark_#{m}, :#{m}
def #{m}(*args, &block)
puts self.class.name + ' ' + self.object_id.to_s + '.#{m}(' + args.map{|a| a.to_s}.join(',') + ')'
_benchmark_#{m}(*args,&block)
end
end
EOR
end
klass.class_eval(overriden_methods.join("\n"), __FILE__, __LINE__)
end
end
i = Benchmark.instance
i.for(Integer, :upto)
i.for(A)
a = A.new
a.sum(5)
a.sum(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment