Created
October 6, 2010 12:46
-
-
Save manveru/613294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
etanni = Etanni.new('Hello, #{name}') | |
d = Struct.new(:name).new('manveru') | |
blocks = [ | |
proc{ | |
etanni.result(d) | |
}, proc{ | |
etanni = Etanni.new('Hello, #{name}') | |
etanni.result(d) | |
}, proc{ | |
etanni = Etanni.new('Hello, #{name}') | |
d = Struct.new(:name).new('manveru') | |
etanni.result(d) | |
} | |
] | |
blocks.combination(2) do |left, right| | |
result = Benchmark.compare_realtime(iterations: 10, &left).with(&right) | |
Benchmark.report_on result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment