Skip to content

Instantly share code, notes, and snippets.

@ggmichaelgo
Last active March 9, 2022 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggmichaelgo/f108a7ab3327ea68f0afe8ab40366c55 to your computer and use it in GitHub Desktop.
Save ggmichaelgo/f108a7ab3327ea68f0afe8ab40366c55 to your computer and use it in GitHub Desktop.
Liquid-C benchmark
# This is a very basic benchmark of Liquid-c rendering.
# Tested with
# Ryzen 5 2600
# 32GB Ram
# Ubuntu 18.04.6 LTS
require "benchmark/ips"
require "liquid"
require "liquid/c"
liquid_template = <<END
{% for product in products %}
<div class='product_brick'>
<div class='container'>
<div class='element'>
<img src='images/{{ product.image }}' class='product_miniature' />
</div>
<div class='element description'>
<a href={{ product.url }} class='product_name block bold'>
{{ product.external_index }}
</a>
</div>
</div>
</div>
{% endfor %}
END
data = {
'products' => 100.times.map do |n|
{ 'image' => "foo-#{rand 100}.png", 'url' => "http://bar-#{rand 100}.com", 'external_index' => "FOO #{rand 100}" }
end
}
def render_liquid(template, data)
Liquid::Template.parse(template).render(data)
end
liquid_template_pre = Liquid::Template.parse(liquid_template)
def render_liquid_pre(tmpl, data)
tmpl.render(data)
end
Benchmark.ips do |x|
x.report('render_liquid') { render_liquid(liquid_template, data) }
x.report('render_liquid_pre') { render_liquid_pre(liquid_template_pre, data) }
end
@ggmichaelgo
Copy link
Author

ggmichaelgo commented Nov 8, 2021

Liquid-C 4.0.0

Warming up --------------------------------------
       render_liquid    93.000  i/100ms
   render_liquid_pre    95.000  i/100ms
Calculating -------------------------------------
       render_liquid    934.684  (± 1.5%) i/s -      4.743k in   5.075563s
   render_liquid_pre    965.393  (± 1.6%) i/s -      4.845k in   5.019884s

Liquid-C 4.1.0

Warming up --------------------------------------
       render_liquid   383.000  i/100ms
   render_liquid_pre   432.000  i/100ms
Calculating -------------------------------------
       render_liquid      3.817k (± 1.0%) i/s -     19.150k in   5.017945s
   render_liquid_pre      4.348k (± 1.8%) i/s -     22.032k in   5.068580s

@ggmichaelgo
Copy link
Author

Mustache vs Liquid

Warming up --------------------------------------
     render_mustache    99.000  i/100ms
 render_mustache_pre   165.000  i/100ms
       render_liquid    89.000  i/100ms
   render_liquid_pre    95.000  i/100ms
Calculating -------------------------------------
     render_mustache    989.726  (± 1.4%) i/s -      4.950k in   5.002351s
 render_mustache_pre      1.655k (± 1.6%) i/s -      8.415k in   5.087024s
       render_liquid    903.586  (± 0.9%) i/s -      4.539k in   5.023754s
   render_liquid_pre    978.205  (± 2.4%) i/s -      4.940k in   5.053165s

Comparison:
 render_mustache_pre:     1654.6 i/s
     render_mustache:      989.7 i/s - 1.67x  (± 0.00) slower
   render_liquid_pre:      978.2 i/s - 1.69x  (± 0.00) slower
       render_liquid:      903.6 i/s - 1.83x  (± 0.00) slower

Mustache vs Liquid-C

Warming up --------------------------------------
     render_mustache   100.000  i/100ms
 render_mustache_pre   168.000  i/100ms
       render_liquid    91.000  i/100ms
   render_liquid_pre    97.000  i/100ms
Calculating -------------------------------------
     render_mustache    997.077  (± 1.5%) i/s -      5.000k in   5.015802s
 render_mustache_pre      1.691k (± 0.8%) i/s -      8.568k in   5.066910s
       render_liquid    908.254  (± 1.0%) i/s -      4.550k in   5.010059s
   render_liquid_pre    983.415  (± 0.7%) i/s -      4.947k in   5.030669s

Comparison:
 render_mustache_pre:     1691.1 i/s
     render_mustache:      997.1 i/s - 1.70x  (± 0.00) slower
   render_liquid_pre:      983.4 i/s - 1.72x  (± 0.00) slower
       render_liquid:      908.3 i/s - 1.86x  (± 0.00) slower

Mustache vs mg-const-table

Warming up --------------------------------------
     render_mustache    98.000  i/100ms
 render_mustache_pre   165.000  i/100ms
       render_liquid   376.000  i/100ms
   render_liquid_pre   429.000  i/100ms
Calculating -------------------------------------
     render_mustache    981.643  (± 1.9%) i/s -      4.998k in   5.093392s
 render_mustache_pre      1.664k (± 0.7%) i/s -      8.415k in   5.058266s
       render_liquid      3.750k (± 1.1%) i/s -     18.800k in   5.014307s
   render_liquid_pre      4.281k (± 1.3%) i/s -     21.450k in   5.011499s

Comparison:
   render_liquid_pre:     4280.9 i/s
       render_liquid:     3749.7 i/s - 1.14x  (± 0.00) slower
 render_mustache_pre:     1663.7 i/s - 2.57x  (± 0.00) slower
     render_mustache:      981.6 i/s - 4.36x  (± 0.00) slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment