Skip to content

Instantly share code, notes, and snippets.

@manveru
Created October 6, 2010 12:56
Show Gist options
  • Select an option

  • Save manveru/613302 to your computer and use it in GitHub Desktop.

Select an option

Save manveru/613302 to your computer and use it in GitHub Desktop.
class Etanni
SEPARATOR = "E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82"
CHOMP = "<<#{SEPARATOR}.chomp!"
START = "\n_out_ << #{CHOMP}\n"
STOP = "\n#{SEPARATOR}\n"
REPLACEMENT = "#{STOP}\\1#{START}"
def initialize(template, filename = '<Etanni>')
@template = template
@filename = filename
compile
end
def compile(filename = @filename)
temp = @template.strip
temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, REPLACEMENT)
@compiled = eval("Proc.new{ _out_ = [#{CHOMP}]\n#{temp}#{STOP}_out_.join }",
nil, @filename)
end
def result(instance, filename = @filename)
instance.instance_eval(&@compiled)
end
end
ENV['R_HOME'] ||= '/usr/lib/R'
require 'better-benchmark'
class BetterBenchmark
attr_reader :reports
def self.bmbm(*args, &block)
new.bmbm(*args, &block)
end
def initialize
@reports = []
end
def bmbm(*args)
yield self
reports.combination(2) do |left, right|
left_title, left_block, right_title, right_block = *left, *right
result = Benchmark.compare_realtime(*args, &left_block).with(&right_block)
puts "\n\n#{left_title} vs #{right_title}"
Benchmark.report_on result
end
end
def report(title, &block)
reports << [title, block]
end
end
etanni = Etanni.new('Hello, #{name}')
d = Struct.new(:name).new('manveru')
BetterBenchmark.bmbm do |bench|
bench.report 'fast' do
etanni.result(d)
end
bench.report 'medium' do
etanni = Etanni.new('Hello, #{name}')
etanni.result(d)
end
bench.report 'slow' do
etanni = Etanni.new('Hello, #{name}')
d = Struct.new(:name).new('manveru')
etanni.result(d)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment