Created
December 21, 2012 15:00
-
-
Save kbrock/4353315 to your computer and use it in GitHub Desktop.
timing custom string interpolation
This file contains hidden or 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
| require "benchmark" | |
| require 'erb' | |
| d=Time.now | |
| Benchmark.bm(20) do |x| | |
| # the ultimate, but still can't figure out how to generate something that can be interpolated | |
| x.report("n - str") { 100_000.times { | |
| "abc #{d.hour < 10 ? '0' : ''}#{d.hour} #{d.min < 10 ? '0' : ''}#{d.min} cdef" | |
| } } | |
| #not dynamically constructed (seing if extra fields slows this down) | |
| x.report("n - code0<<") { 100_000.times { | |
| 'abc ' << (d.hour < 10 ? '0' : '') << d.send(:hour).to_s << ' ' << (d.hour < 10 ? '0' : '') << d.send(:min).to_s << ' cdef' | |
| } } | |
| #using formatted % formatters | |
| x.report("y - 2%") { 100_000.times { | |
| 'abc %2.2d %2.2d cdef' % [d.hour, d.min] | |
| } } | |
| # using a static proc to generate the output | |
| ops=proc {|d| 'abc ' << (d.hour < 10 ? '0' : '') << d.send(:hour).to_s << ' ' << (d.hour < 10 ? '0' : '') << d.send(:min).to_s << ' cdef' } | |
| x.report("y - proc{}") { 100_000.times { | |
| ops.call(d) | |
| } } | |
| # using eval to generate the proc with formatters | |
| x.report("Y - proc{}.e pre") { 100_000.times { | |
| ops=eval %{proc {|d| 'abc ' << (d.hour < 10 ? '0' : '') << d.send(:hour).to_s << ' ' << (d.hour < 10 ? '0' : '') << d.send(:min).to_s << ' cdef' }} | |
| } } | |
| x.report("Y - proc{}.e") { 100_000.times { | |
| ops.call(d) | |
| } } | |
| ops = proc {|d| 'abc %2.2d %2.2d cdef' % [d.hour, d.min] } | |
| x.report("y - proc{%}") { 100_000.times { | |
| ops.call(d) | |
| } } | |
| x.report("Y - strftime") { 100_000.times { | |
| d.strftime 'abc %H %M cdef' | |
| } } | |
| x.report("Y - proc{%}.e prep") { 100_000.times { | |
| ops=eval %{proc {|dd| 'abc %2.2d %2.2d cdef' % [dd.hour, dd.min]}} | |
| } } | |
| ops=eval %{proc {|dd| 'abc %2.2d %2.2d cdef' % [dd.hour, dd.min]}} | |
| x.report("Y - proc{%}.e") { 100_000.times { | |
| ops.call(d) | |
| } } | |
| ops=%w(hour min) | |
| x.report("y - %[]") { 100_000.times { | |
| 'abc %2.2d %2.2d cdef' % ops.map {|v| d.send(v)} | |
| } } | |
| ops=[:hour, :min] | |
| x.report("y - %[?]") { 100_000.times { | |
| values=ops.map {|v| v.is_a?(Symbol) ? d.send(v) : v } | |
| 'abc %2.2d %2.2d cdef' % values | |
| } } | |
| x.report("Y - %proc[] prep") { 100_000.times { | |
| ops=[] << proc {|dd| dd.hour} << proc {|dd| dd.min} | |
| } } | |
| ops=[] << proc {|dd| dd.hour} << proc {|dd| dd.min} | |
| x.report("Y - %proc[]") { 100_000.times { | |
| 'abc %2.2d %2.2d cdef' % ops.map { |v| v.call(d)} | |
| } } | |
| x.report("Y - %proc[1]e prep") { 100_000.times { | |
| ops=eval %{proc {|dd| [dd.hour, dd.min] } } | |
| } } | |
| x.report("Y - %proc[1]e") { 100_000.times { | |
| 'abc %2.2d %2.2d cdef' % ops.call(d) | |
| } } | |
| x.report("y - hash%") { 100_000.times { | |
| 'abc %2.2<hour>d %2.2<min>d cdef' % { hour: d.hour, min: d.min } | |
| } } | |
| ops=%w(hour min) | |
| x.report("y - hash%[]") { 100_000.times { | |
| values={} ; ops.each {|op| values[op.to_sym]=d.send(op) } | |
| 'abc %2.2<hour>d %2.2<min>d cdef' % values | |
| } } | |
| # ops='abc #{hour} #{min} cdef' | |
| # x.report("N - template1") { 100_000.times { | |
| # ops.gsub(/\#\{(\w+)\}/) { d.send($1) } | |
| # } } | |
| x.report("Y - eval") { 100_000.times { | |
| eval '"abc #{d.hour < 10 ? "0" : ""}#{d.hour} #{d.min < 10 ? "0" : ""}#{d.min} cdef"' | |
| } } | |
| x.report("Y - erb pre") { 100_000.times { | |
| ops=ERB.new("abc <%= d.hour %> <%= d.min %> cdef") | |
| } } | |
| x.report("Y - erb") { 100_000.times { | |
| ops.result(binding) | |
| } } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment