Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active August 29, 2015 14:08
Show Gist options
  • Save mbbx6spp/28dc76c0f72d746bc3ff to your computer and use it in GitHub Desktop.
Save mbbx6spp/28dc76c0f72d746bc3ff to your computer and use it in GitHub Desktop.
Simple ERB micro benchmarks to (in)validate my assumptions of readable vs "more performant" templating code. Basically I had to eat my words.
require 'erb'
require 'minitest/benchmark'
require 'minitest/autorun'
describe "ERB" do
let(:host) do
"localhost"
end
let(:port) do
"8080"
end
describe "using multiple blocks" do
let(:erb) do
ERB.new("<%= host %>:<%= port %>")
end
bench_performance_constant "to render host and port" do
erb.result(binding)
end
end
describe "using concatenated strings in one block" do
let(:erb) do
ERB.new("<%= host + ':' + port %>")
end
bench_performance_constant "to render host and port" do
erb.result(binding)
end
end
describe "using one string in one block" do
let(:erb) do
ERB.new("<%= hoststring %>")
end
let(:hoststring) { host + ':' + port }
bench_performance_constant "to render hoststring" do
erb.result(binding)
end
end
end

Results of the benchmark on my MBP with Ruby v1.9.3-p545 looked like this:

$ ruby erb_benchmark.rb                                                                                      
Run options: --seed 1619

# Running tests:



Finished tests in 0.000771s, 0.0000 tests/s, 0.0000 assertions/s.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

# Running benchmarks:


ERB::using concatenated strings in one block    1       10      100     1000    10000
bench_to_render_host_and_port    0.000182        0.000032        0.000029        0.000028        0.000029

ERB::using multiple blocks      1       10      100     1000    10000
bench_to_render_host_and_port    0.000113        0.000034        0.000032        0.000031        0.000031

ERB::using one string in one block       1       10      100     1000    10000
bench_to_render_hoststring       0.000080        0.000027        0.000037        0.000024        0.000025


Finished benchmarks in 0.012297s, 243.9619 tests/s, 243.9619 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips

Another run:

$ ruby erb_benchmark.rb 
Run options: --seed 34951

# Running tests:



Finished tests in 0.000787s, 0.0000 tests/s, 0.0000 assertions/s.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

# Running benchmarks:


ERB Benchmark::using concatenated strings in one block  1       10      100     1000    10000
bench_to_render_host_and_port    0.000147        0.000032        0.000030        0.000030        0.000030

ERB Benchmark::using multiple blocks    1       10      100     1000    10000
bench_to_render_host_and_port    0.000101        0.000034        0.000032        0.000042        0.000032

ERB Benchmark::using one string in one block     1       10      100     1000    10000
bench_to_render_hoststring       0.000086        0.000032        0.000027        0.000026        0.000026


Finished benchmarks in 0.012290s, 244.1009 tests/s, 244.1009 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips

And another run:

$ ruby erb_benchmark.rb 
Run options: --seed 49169

# Running tests:



Finished tests in 0.000742s, 0.0000 tests/s, 0.0000 assertions/s.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

# Running benchmarks:


ERB Benchmark::using concatenated strings in one block  1       10      100     1000    10000
bench_to_render_host_and_port    0.000145        0.000034        0.000040        0.000030        0.000029

ERB Benchmark::using multiple blocks    1       10      100     1000    10000
bench_to_render_host_and_port    0.000104        0.000049        0.000043        0.000035        0.000034

ERB Benchmark::using one string in one block     1       10      100     1000    10000
bench_to_render_hoststring       0.000095        0.000030        0.000029        0.000026        0.000030


Finished benchmarks in 0.012543s, 239.1772 tests/s, 239.1772 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment