[Boulder Ruby Group] Builder is slow
Tony Arcieri
tony at clickcaster.com
Tue Jul 17 14:53:59 EDT 2007
Last night I decided to hack together a more-or-less Builder-compatible
frontend to libxml-ruby after dealing with the slowness of Builder. I was
curious where the bottlenecks were. Builder obviously makes extensive use
of method_missing, but after testing out method_missing I decided it wasn't
that slow:
10 million normal method calls:
3.620000 0.000000 3.620000 ( 3.656555)
10 million method_missing calls:
4.190000 0.010000 4.200000 ( 4.193837)
Other than that, Builder just does a lot of tag construction and is
constantly appending to an output buffer. I didn't really see a better way
to do this short of a native extension, so I decided to check out
libxml-ruby's XML construction and try to write something Builder-compatible
which uses it.
I put together a simple proof-of-concept to see whether using libxml-ruby
could improve the performance:
http://pastie.caboo.se/79575
Not the most elegant thing in the world, but the benchmarks speak for
themselves. Here's 3 runs of each, both from a fresh irb:
libxml-ruby based proof-of-concept:
>> puts Benchmark.measure { xml = XmlBase.new; xml.foo(:a => :b) { 100.times{
xml.bar(:c => :d) { 1000.times { xml.baz("Text goes here") } } } }.to_s }
3.130000 1.290000 4.420000 ( 4.433537)
2.850000 1.280000 4.130000 ( 4.133377)
2.810000 1.250000 4.060000 ( 4.070264)
Builder:
>> puts Benchmark.measure { xml = Builder::XmlMarkup.new; xml.foo(:a => :b)
{ 100.times { xml.bar(:c => :d) { 1000.times { xml.baz("Text goes here") } }
} } }
9.300000 0.030000 9.330000 ( 9.374801)
9.290000 0.020000 9.310000 ( 9.315121)
9.280000 0.020000 9.300000 ( 9.316049)
Using libxml-ruby is over twice as fast while still preserving API
compatibility. It looks like Builder's slowdown comes from the actual tag
construction/string appending being done in Ruby.
--
Tony Arcieri
ClickCaster, Inc.
tony at clickcaster.com
720-227-0129 ext. 202
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/bdrg-members/attachments/20070717/c33fdbb0/attachment.html
More information about the Bdrg-members
mailing list