-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is faster: assign or capture? #487
Comments
I guess assign based on it doing the same as capture except capture has all the extra logica for looping through all the block data. |
require "liquid"
require "benchmark"
template1 = '{% capture foobar %}foo{{ bar }}{% endcapture %}{{ foo }}{{ foobar }}'
template2 = '{% assign foobar = "foo" | append: bar %}{{ foobar }}'
def render(template)
Liquid::Template.parse(template).render("bar" => "42")
end
puts render(template1)
puts render(template2)
n = 100_000
Benchmark.bm(10) do |b|
b.report("capture") do
n.times do
render(template1)
end
end
b.report("assign") do
n.times do
render(template2)
end
end
end
|
@fw42 This is awesome. Now I know how to profile tags. |
@fw42 In the line 4 you have a require "liquid"
require "benchmark"
template1 = '{% capture foobar %}foo{{ bar }}{% endcapture %}{{ foobar }}'
template2 = '{% assign foobar = "foo" | append: bar %}{{ foobar }}'
def render(template)
Liquid::Template.parse(template).render("bar" => "42")
end
puts render(template1)
puts render(template2)
n = 100_000
Benchmark.bm 10 do |b|
b.report "capture" do
n.times do
render template1
end
end
b.report "assign" do
n.times do
render template2
end
end
end Results on my desktop: #1
user system total real
capture 13.160000 0.000000 13.160000 ( 13.166675)
assign 18.190000 0.170000 18.360000 ( 18.440864)
# 2
user system total real
capture 13.310000 0.000000 13.310000 ( 13.332725)
assign 18.140000 0.000000 18.140000 ( 18.167654)
# 3
user system total real
capture 13.230000 0.000000 13.230000 ( 13.243090)
assign 18.020000 0.200000 18.220000 ( 18.332655) Surprisingly the |
oh good catch |
strange, I would not have expected capture to be faster |
I'd be interested in knowing this for Liquid-C as well, will give it a test. |
They are similar with Liquid-C:
(my computer gets these results without C):
So it looks like the slowness of assign is from the extra complexity in the parsing step. |
Adding another template1 = '{% capture foobar %}foo{{ bar }}baz{% endcapture %}{{ foobar }}'
template2 = '{% assign foobar = "foo" | append: bar | append: "baz" %}{{ foobar }}' foo42baz
foo42baz
user system total real
capture 13.390000 0.000000 13.390000 ( 13.403049)
assign 21.320000 0.160000 21.480000 ( 21.489370) |
@penibelst which version of Liquid are you using? I get this with the extra append:
and with C:
|
@pushrax 2.6.1 |
Can you try against 3.0.0? I don't really expect a huge difference, but it's still worthwhile. |
v3.0.0
foo42
foo42
user system total real
capture 14.880000 0.000000 14.880000 ( 14.890935)
assign 16.330000 0.030000 16.360000 ( 16.374504)
# with extra append
v3.0.0
foo42baz
foo42baz
user system total real
capture 15.450000 0.060000 15.510000 ( 15.525165)
assign 19.170000 0.170000 19.340000 ( 19.411027) |
does ruby "benchmark" still exist?
thanks from two years ago |
@dyspop I believe benchmark is bundled in the Ruby stdlib now. No gem needed 😄 |
huzzah |
Using require "liquid"
require "benchmark/ips"
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
puts "Liquid #{Liquid::VERSION}"
template1 = '{% capture foobar %}foo{{ bar }}{% endcapture %}{{ foo }}{{ foobar }}'
template2 = '{% assign foobar = "foo" | append: bar %}{{ foobar }}'
def render(template)
Liquid::Template.parse(template).render("bar" => "42")
end
puts render(template1)
puts render(template2)
Benchmark.ips do |x|
x.report("capture") { render(template1) }
x.report("assign") { render(template2) }
end
|
Very old issue, but in case it is useful to anyone in the future, I used the benchmark-ips test above from @parkr to test this again, wondering if anything had changed with this in the last 8 years, and for me locally on ruby 3.2.2 it shows assign as now 20% faster:
|
I’m curious which procedure runs faster. Given is the variable
bar
.Capture
Assign
The text was updated successfully, but these errors were encountered: