Created
May 31, 2024 11:16
-
-
Save makenowjust/b4852a59e53f0c85c740818c75303d5e to your computer and use it in GitHub Desktop.
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 "rexml/document" | |
n = 10000 | |
xml = REXML::Document.new(<<~XML) | |
<?xml version="1.0" encoding="UTF-8"?> | |
<breakfast_menu> | |
<food> | |
<name>Belgian Waffles</name> | |
<price>$5.95</price> | |
<description> | |
Two of our famous Belgian Waffles with plenty of real maple syrup | |
</description> | |
<calories>650</calories> | |
</food> | |
<food> | |
<name>Strawberry Belgian Waffles</name> | |
<price>$7.95</price> | |
<description> | |
Light Belgian waffles covered with strawberries and whipped cream | |
</description> | |
<calories>900</calories> | |
</food> | |
<food> | |
<name>Berry-Berry Belgian Waffles</name> | |
<price>$8.95</price> | |
<description> | |
Belgian waffles covered with assorted fresh berries and whipped cream | |
</description> | |
<calories>900</calories> | |
</food> | |
<food> | |
<name>French Toast</name> | |
<price>$4.50</price> | |
<description> | |
Thick slices made from our homemade sourdough bread | |
</description> | |
<calories>600</calories> | |
</food> | |
<food> | |
<name>Homestyle Breakfast</name> | |
<price>$6.95</price> | |
<description> | |
Two eggs, bacon or sausage, toast, and our ever-popular hash browns | |
</description> | |
<calories>950</calories> | |
</food> | |
</breakfast_menu> | |
XML | |
def my_each_recursive(element, &) | |
element.each_child do |child| | |
next unless child.is_a?(REXML::Element) | |
yield child | |
my_each_recursive(child, &) | |
end | |
end | |
es1 = [] | |
xml.each_recursive { es1 << _1 } | |
es2 = [] | |
my_each_recursive(xml) { es2 << _1 } | |
raise "invalid implementation" unless es1 == es2 | |
Benchmark.bm do |x| | |
x.report(" each_recursive") { n.times { xml.each_recursive { } } } | |
x.report("my_each_recursive") { n.times { my_each_recursive(xml) { } } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment