Skip to content

Commit cc4769c

Browse files
committed
Add comparison to benchmarks
This sorts the runs in order of speed and shows the relative speed difference: ``` Comparison: addition2: 24011974.8 i/s addition3: 23958619.8 i/s - 1.00x slower addition-test-long-label: 5014756.0 i/s - 4.79x slower addition: 4955278.9 i/s - 4.85x slower ```
1 parent 064de5a commit cc4769c

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

benchmarks/run-benchmarks.rb

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class SlimBenchmarks
1515
def initialize(slow)
16-
@benches = []
16+
@benches = Hash.new { |h, k| h[k] = [] }
1717

1818
@erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
1919
@haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
@@ -41,14 +41,14 @@ def run_slim_pretty; #{Slim::Engine.new(pretty: true).call @slim_code}; end
4141
def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
4242
}
4343

44-
bench('(1) erb') { context.run_erb }
45-
bench('(1) erubis') { context.run_erubis }
46-
bench('(1) fast erubis') { context.run_fast_erubis }
47-
bench('(1) temple erb') { context.run_temple_erb }
48-
bench('(1) slim pretty') { context.run_slim_pretty }
49-
bench('(1) slim ugly') { context.run_slim_ugly }
50-
bench('(1) haml pretty') { context.run_haml_pretty }
51-
bench('(1) haml ugly') { context.run_haml_ugly }
44+
bench(:compiled, 'erb') { context.run_erb }
45+
bench(:compiled, 'erubis') { context.run_erubis }
46+
bench(:compiled, 'fast erubis') { context.run_fast_erubis }
47+
bench(:compiled, 'temple erb') { context.run_temple_erb }
48+
bench(:compiled, 'slim pretty') { context.run_slim_pretty }
49+
bench(:compiled, 'slim ugly') { context.run_slim_ugly }
50+
bench(:compiled, 'haml pretty') { context.run_haml_pretty }
51+
bench(:compiled, 'haml ugly') { context.run_haml_ugly }
5252
end
5353

5454
def init_tilt_benches
@@ -62,47 +62,54 @@ def init_tilt_benches
6262

6363
context = Context.new
6464

65-
bench('(2) erb') { tilt_erb.render(context) }
66-
bench('(2) erubis') { tilt_erubis.render(context) }
67-
bench('(2) temple erb') { tilt_temple_erb.render(context) }
68-
bench('(2) slim pretty') { tilt_slim_pretty.render(context) }
69-
bench('(2) slim ugly') { tilt_slim_ugly.render(context) }
70-
bench('(2) haml pretty') { tilt_haml_pretty.render(context) }
71-
bench('(2) haml ugly') { tilt_haml_ugly.render(context) }
65+
bench(:tilt, 'erb') { tilt_erb.render(context) }
66+
bench(:tilt, 'erubis') { tilt_erubis.render(context) }
67+
bench(:tilt, 'temple erb') { tilt_temple_erb.render(context) }
68+
bench(:tilt, 'slim pretty') { tilt_slim_pretty.render(context) }
69+
bench(:tilt, 'slim ugly') { tilt_slim_ugly.render(context) }
70+
bench(:tilt, 'haml pretty') { tilt_haml_pretty.render(context) }
71+
bench(:tilt, 'haml ugly') { tilt_haml_ugly.render(context) }
7272
end
7373

7474
def init_parsing_benches
7575
context = Context.new
7676
context_binding = context.instance_eval { binding }
7777

78-
bench('(3) erb') { ERB.new(@erb_code).result(context_binding) }
79-
bench('(3) erubis') { Erubis::Eruby.new(@erb_code).result(context_binding) }
80-
bench('(3) fast erubis') { Erubis::FastEruby.new(@erb_code).result(context_binding) }
81-
bench('(3) temple erb') { Temple::ERB::Template.new { @erb_code }.render(context) }
82-
bench('(3) slim pretty') { Slim::Template.new(pretty: true) { @slim_code }.render(context) }
83-
bench('(3) slim ugly') { Slim::Template.new { @slim_code }.render(context) }
84-
bench('(3) haml pretty') { Haml::Engine.new(@haml_code, format: :html5).render(context) }
85-
bench('(3) haml ugly') { Haml::Engine.new(@haml_code, format: :html5, ugly: true).render(context) }
78+
bench(:parsing, 'erb') { ERB.new(@erb_code).result(context_binding) }
79+
bench(:parsing, 'erubis') { Erubis::Eruby.new(@erb_code).result(context_binding) }
80+
bench(:parsing, 'fast erubis') { Erubis::FastEruby.new(@erb_code).result(context_binding) }
81+
bench(:parsing, 'temple erb') { Temple::ERB::Template.new { @erb_code }.render(context) }
82+
bench(:parsing, 'slim pretty') { Slim::Template.new(pretty: true) { @slim_code }.render(context) }
83+
bench(:parsing, 'slim ugly') { Slim::Template.new { @slim_code }.render(context) }
84+
bench(:parsing, 'haml pretty') { Haml::Engine.new(@haml_code, format: :html5).render(context) }
85+
bench(:parsing, 'haml ugly') { Haml::Engine.new(@haml_code, format: :html5, ugly: true).render(context) }
8686
end
8787

8888
def run
89-
Benchmark.ips do |x|
90-
@benches.each do |name, block|
91-
x.report(name.to_s, &block)
89+
@benches.each do |group_name, group_benches|
90+
puts "Running #{group_name} benchmarks:"
91+
92+
Benchmark.ips do |x|
93+
group_benches.each do |name, block|
94+
x.report("#{group_name} #{name}", &block)
95+
end
96+
97+
x.compare!
9298
end
9399
end
100+
94101
puts "
95-
(1) Compiled benchmark. Template is parsed before the benchmark and
102+
Compiled benchmark: Template is parsed before the benchmark and
96103
generated ruby code is compiled into a method.
97104
This is the fastest evaluation strategy because it benchmarks
98105
pure execution speed of the generated ruby code.
99106
100-
(2) Compiled Tilt benchmark. Template is compiled with Tilt, which gives a more
107+
Compiled Tilt benchmark: Template is compiled with Tilt, which gives a more
101108
accurate result of the performance in production mode in frameworks like
102109
Sinatra, Ramaze and Camping. (Rails still uses its own template
103110
compilation.)
104111
105-
(3) Parsing benchmark. Template is parsed every time.
112+
Parsing benchmark: Template is parsed every time.
106113
This is not the recommended way to use the template engine
107114
and Slim is not optimized for it. Activate this benchmark with 'rake bench slow=1'.
108115
@@ -111,8 +118,8 @@ def run
111118
"
112119
end
113120

114-
def bench(name, &block)
115-
@benches.push([name, block])
121+
def bench(group, name, &block)
122+
@benches[group].push([name, block])
116123
end
117124
end
118125

0 commit comments

Comments
 (0)