Here’s some stuff from the #ruby-lang mailinglist, with my replies inlined. It’s here because this kind of discussion often doesn’t go far on a mailinglist, but the response may have some validity to some people.
There really isn’t a secret list of “idioms” or rules that will save you from writing slow software. You really have to profile, and more than that, you need to focus on the right stuff, at the right time. I once found this hard to swallow, but I understand it, and it makes perfect sense. Being free of it, I now write better software, in a much shorter time frame. Oh and it’s easier to maintain too.
On 17 Mar 2008, at 18:08, Paul Brannan wrote:
On Tue, Mar 18, 2008 at 02:17:22AM +0900, Gerardo Santana G?mez Garrido wrote:
On Mon, Mar 17, 2008 at 12:42 PM, Marc Heiler wrote:
Anyone of you has a few hints on how to speed up ruby code?
(Note – not writing it, but running it
)
I only have a few hints, like 5… would love to extend it.
When generating text output, using StringIO is faster than using puts
This doesn’t make sense. One can easily call #puts on a StringIO
object.
Do you have a concrete example/benchmark that demonstrates the
difference?
Paul
This is a prime example of platform differences, hell, right up to the tool stack you’re running.
Generic ‘optimizations’ or hints in areas like this are not worth their salt. It’ll be different in one console / terminal / operating system to the next.
Moreover, a much better solution is to rebuffer the data, and specifically to coalesce it, in many cases (not all). This is not something that a generic “#freeze” “optimization” will give you. It’s a design decision, the overhead for which can be discovered by profiling if you spend some time thinking about what the numbers mean, and paying attention to how long you really think things should take. It doesn’t take long to realize that $stdout.puts is slow on a lot, but not *all* platforms. The simplest test is redirect to file, which on some setups, is *significantly* faster than terminal output (one is to memory, one is to disk, and yet…. go figure).
On 17 Mar 2008, at 15:06, Paul Brannan wrote:
On Mon, Mar 17, 2008 at 11:20:59PM +0900, Avdi Grimm wrote:
On Mon, Mar 17, 2008 at 9:22 AM, Ilan Berci coder68 @yahoo.com wrote:
There is only 1 hint:
PROFILE IT!!!
+1
is +10 allowed?
I learned this lesson, and it is a serious lesson to learn.
There was a time when I had a feeling of perfection, where I wanted the code to be entirely as computationally optimal as possible all the time. The truth is, the stack underneath contains so much indirection and complexity, this is almost impossible to really attain, and indeed to do so, one has to profile anyway. The thing that solved this for me was including a notion of the development process, time, and simplicity into my notion of perfection.
> All discussions of performance tuning are meaningless without profiling numbers.
Profiling only tells you where performance bottlenecks are. It does
nothing for letting you know how to fix those bottlenecks. That’s where
benchmarks come into play.
Sure, but that’s algorithm and even state dependent. Maybe you can get 10% by adding a GC.start after loading a pile of data (this would be very app specific).
Benchmarks can lie (if poorly written, and it’s all too easy to write bad benchmarks), for example, on win32, it is possible to construct a benchmark under which the conditions will cause the ternary operator to appear faster than if else. Obviously, this is not *really* the case, moreover, it’s completely irrelevant unless you’re app is entirely bound by boolean logic operations. It’s also extremely difficult to use a profiling or benchmarking tool against, in pure ruby. Oh, and I’d put good money on it not having the same results on different versions or different interpreters.
There are limits to how low you can measure performance and a large portion of the discussed list falls into that category. It also seems to fall in to the category of stuff that is:
- interpreter specific
- implementation dependent (meaning you could kill yourself if you migrate versions even)
- only going to gain you 0.001% or something equally irrelevant.
If you really want to continue on that list, you’ll want to include stuff like the following (which I don’t really agree with as good ideas to spread across the board):
- implicit return if faster
- implicit block break values are faster
- inject is ’super slow’
- splats are slow
- sprintf is slow
These all show up in micro-benchmarks for sure. They don’t necessarily (in fact, “very often”) show up as real sources of poor performance in production code, unless you have very small units (in which case, good for you
). Poor performance in common application code tends to come from complexity most often. In some areas (heavier I/O code is maybe the most common?) you may need to add complexity, but it’s often possible to provide a drop-in piece of indirection (off the top of my head: cache, coalesce, un-block, drop threaded complexity, buffer, *increase buffer sizes*, and so on).
There was someone talking about ERB#new. That’s ERB.new… and for performance, Erubis is often faster, so there’s no real need to do much profiling there, as someone’s done it for you, as one other example of design change leading the real win.
Design change is really what has a significant effect on performance, in most all cases. Very few ‘generic’ things like concat vs. + for strings (which is pretty clear when profiling) really give you meaningful gains, although it does happen (this was a ~10,000 page pdf being generated iirc):
Original

This shows nasty saw-tooth ram usage, and poor memory performance. That red line is the kernel sorting out memory.
Mine

This was actually something pretty equivalent to s/” + “/” << “/g for this particular case, this will not come along very often
Here’s something I used to replace ‘builder’ for something that needed more speed:
X = Object.new
def X.[](name, att = nil, inner = nil)
inner ? att ? "< #{name}#{att}>#{inner}" : "< #{name}>#{inner}" : att ? "< #{name}#{att} />" : "< #{name}/>"
end
Clearly the api is not the same, and it doesn’t do nearly the same stuff. It’s an extreme example. However, it smashes over everything else for performance (when combined with concat, not +, clearly), so we no longer have to care. Maintenance is not proving any kind of problem either, as we don’t use it everywhere. We use this *only* where we need to, which in our case was a very long XML generation (in excess of 20mb with very high node count).
The example has the following different profiles, which I would strongly argue simply could not be obtained by sticking with a lib with builders features (that is, this isn’t builders fault, nor is the above a replacement for builder. It’s one-time build and use code for a few cases where it’s needed):
Total Time: 4.452756
%total %self total self wait child calls Name
--------------------------------------------------------------------------------
100.00% 0.00% 4.45 0.00 0.00 4.45 0 Global#[No method] (prof_vs_frozen.rb:55} prof_vs_frozen.rb:55
4.45 2.01 0.00 2.44 1/1 Integer#times
--------------------------------------------------------------------------------
4.45 2.01 0.00 2.44 1/1 Global#[No method]
100.00% 45.16% 4.45 2.01 0.00 2.44 1 Integer#times (ruby_runtime:0} ruby_runtime:0
2.01 2.01 0.00 0.00 200000/200000 <Object::Object>#[]
0.22 0.22 0.00 0.00 100000/100000 Array#<<
0.22 0.22 0.00 0.00 100000/100000 String#==
--------------------------------------------------------------------------------
2.01 2.01 0.00 0.00 200000/200000 Integer#times
45.05% 45.05% 2.01 2.01 0.00 0.00 200000 <Object::Object>#[] (prof_vs_frozen.rb:44} prof_vs_frozen.rb:44
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/100000 Integer#times
4.92% 4.92% 0.22 0.22 0.00 0.00 100000 String#== (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/100000 Integer#times
4.87% 4.87% 0.22 0.22 0.00 0.00 100000 Array#<< (ruby_runtime:0} ruby_runtime:0
Thread ID: 214780
Total Time: 116.686341
%total %self total self wait child calls Name
--------------------------------------------------------------------------------
100.00% 0.00% 116.69 0.00 0.00 116.69 0 Global#[No method] (prof_vs_frozen.rb:85} prof_vs_frozen.rb:85
116.69 0.64 0.00 116.04 1/1 Integer#times
--------------------------------------------------------------------------------
116.69 0.64 0.00 116.04 1/1 Global#[No method]
100.00% 0.55% 116.69 0.64 0.00 116.04 1 Integer#times (ruby_runtime:0} ruby_runtime:0
116.04 6.50 0.00 109.55 100000/100000 Builder::XmlBase#method_missing
--------------------------------------------------------------------------------
116.04 6.50 0.00 109.55 100000/100000 Integer#times
99.45% 5.57% 116.04 6.50 0.00 109.55 100000 Builder::XmlBase#method_missing (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:37} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:37
0.22 0.22 0.00 0.00 100000/200000 Kernel#kind_of?
2.12 1.70 0.00 0.42 200000/400000 Builder::XmlBase#_newline
39.69 2.32 0.00 37.37 100000/200000 Builder::XmlMarkup#_start_tag
2.16 1.66 0.00 0.49 100000/600000 Array#each
61.31 1.56 0.00 59.75 100000/100000 Builder::XmlBase#_nested_structures
0.22 0.22 0.00 0.00 100000/200000 NilClass#nil?
1.48 0.97 0.00 0.51 100000/200000 Builder::XmlMarkup#_end_tag
2.13 1.71 0.00 0.42 200000/400000 Builder::XmlBase#_indent
0.22 0.22 0.00 0.00 100000/200000 Array#first
--------------------------------------------------------------------------------
39.69 2.32 0.00 37.37 100000/200000 Builder::XmlBase#method_missing
39.77 2.30 0.00 37.47 100000/200000 Builder::XmlBase#method_missing-1
68.10% 3.96% 79.46 4.62 0.00 74.84 200000 Builder::XmlMarkup#_start_tag (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:286} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:286
0.56 0.56 0.00 0.00 200000/600000 Symbol#to_s
0.93 0.93 0.00 0.00 400000/800000 String#< <
73.35 3.39 0.00 69.96 200000/200000 Builder::XmlMarkup#_insert_attributes
--------------------------------------------------------------------------------
73.35 3.39 0.00 69.96 200000/200000 Builder::XmlMarkup#_start_tag
62.86% 2.91% 73.35 3.39 0.00 69.96 200000 Builder::XmlMarkup#_insert_attributes (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:299} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:299
0.42 0.42 0.00 0.00 200000/600000 Array#each
0.44 0.44 0.00 0.00 200000/200000 Kernel#nil?
69.10 3.75 0.00 65.35 200000/200000 Hash#each
--------------------------------------------------------------------------------
69.10 3.75 0.00 65.35 200000/200000 Builder::XmlMarkup#_insert_attributes
59.22% 3.21% 69.10 3.75 0.00 65.35 200000 Hash#each (ruby_runtime:0} ruby_runtime:0
62.90 3.13 0.00 59.76 200000/200000 Builder::XmlMarkup#_attr_value
0.54 0.54 0.00 0.00 200000/600000 Symbol#to_s
0.49 0.49 0.00 0.00 200000/800000 String#<<
1.43 1.01 0.00 0.42 200000/200000 Enumerable#member?
--------------------------------------------------------------------------------
62.90 3.13 0.00 59.76 200000/200000 Hash#each
53.90% 2.68% 62.90 3.13 0.00 59.76 200000 Builder::XmlMarkup#_attr_value (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:310} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:310
0.44 0.44 0.00 0.00 200000/400000 Module#===
58.89 1.83 0.00 57.07 200000/200000 Builder::XmlBase#_escape_quote
0.43 0.43 0.00 0.00 200000/200000 String#to_s
--------------------------------------------------------------------------------
61.31 1.56 0.00 59.75 100000/100000 Builder::XmlBase#method_missing
52.55% 1.34% 61.31 1.56 0.00 59.75 100000 Builder::XmlBase#_nested_structures (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:132} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:132
0.21 0.21 0.00 0.00 100000/200000 Fixnum#-
59.33 0.97 0.00 58.36 100000/100000 Proc#call
0.21 0.21 0.00 0.00 100000/200000 Fixnum#+
--------------------------------------------------------------------------------
59.33 0.97 0.00 58.36 100000/100000 Builder::XmlBase#_nested_structures
50.84% 0.83% 59.33 0.97 0.00 58.36 100000 Proc#call (ruby_runtime:0} ruby_runtime:0
58.36 6.57 0.00 51.79 100000/100000 Builder::XmlBase#method_missing-1
--------------------------------------------------------------------------------
58.89 1.83 0.00 57.07 200000/200000 Builder::XmlMarkup#_attr_value
50.47% 1.57% 58.89 1.83 0.00 57.07 200000 Builder::XmlBase#_escape_quote (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:118} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:118
0.65 0.65 0.00 0.00 200000/200000 String#gsub
56.41 1.30 0.00 55.12 200000/200000 Builder::XmlBase#_escape
--------------------------------------------------------------------------------
58.36 6.57 0.00 51.79 100000/100000 Proc#call
50.02% 5.63% 58.36 6.57 0.00 51.79 100000 Builder::XmlBase#method_missing-1 (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:37} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:37
0.22 0.22 0.00 0.00 100000/200000 Kernel#kind_of?
2.12 1.70 0.00 0.42 200000/400000 Builder::XmlBase#_newline
39.77 2.30 0.00 37.47 100000/200000 Builder::XmlMarkup#_start_tag
2.18 1.69 0.00 0.49 100000/600000 Array#each
3.43 1.56 0.00 1.87 100000/100000 Builder::XmlBase#_nested_structures-1
0.22 0.22 0.00 0.00 100000/200000 NilClass#nil?
1.49 0.98 0.00 0.51 100000/200000 Builder::XmlMarkup#_end_tag
2.13 1.71 0.00 0.42 200000/400000 Builder::XmlBase#_indent
0.22 0.22 0.00 0.00 100000/200000 Array#first
--------------------------------------------------------------------------------
56.41 1.30 0.00 55.12 200000/200000 Builder::XmlBase#_escape_quote
48.35% 1.11% 56.41 1.30 0.00 55.12 200000 Builder::XmlBase#_escape (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:114} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:114
55.12 2.26 0.00 52.85 200000/200000 String#to_xs
--------------------------------------------------------------------------------
55.12 2.26 0.00 52.85 200000/200000 Builder::XmlBase#_escape
47.24% 1.94% 55.12 2.26 0.00 52.85 200000 String#to_xs (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xchar.rb:110} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xchar.rb:110
0.59 0.59 0.00 0.00 200000/200000 Array#join
51.68 3.45 0.00 48.23 200000/200000 Array#map
0.59 0.59 0.00 0.00 200000/200000 String#unpack
--------------------------------------------------------------------------------
51.68 3.45 0.00 48.23 200000/200000 String#to_xs
44.29% 2.96% 51.68 3.45 0.00 48.23 200000 Array#map (ruby_runtime:0} ruby_runtime:0
48.23 19.46 0.00 28.77 600000/600000 Fixnum#xchr
--------------------------------------------------------------------------------
48.23 19.46 0.00 28.77 600000/600000 Array#map
41.33% 16.68% 48.23 19.46 0.00 28.77 600000 Fixnum#xchr (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xchar.rb:93} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xchar.rb:93
11.46 7.74 0.00 3.72 1800000/1800000 Kernel#===
1.26 1.26 0.00 0.00 600000/600000 Fixnum#<
1.56 1.56 0.00 0.00 600000/600000 Integer#chr
7.86 5.28 0.00 2.58 1200000/1200000 Hash#[]
6.63 4.10 0.00 2.53 600000/600000 Range#===
--------------------------------------------------------------------------------
11.46 7.74 0.00 3.72 1800000/1800000 Fixnum#xchr
9.82% 6.63% 11.46 7.74 0.00 3.72 1800000 Kernel#=== (ruby_runtime:0} ruby_runtime:0
3.72 3.72 0.00 0.00 1800000/2600000 Fixnum#==
--------------------------------------------------------------------------------
7.86 5.28 0.00 2.58 1200000/1200000 Fixnum#xchr
6.73% 4.53% 7.86 5.28 0.00 2.58 1200000 Hash#[] (ruby_runtime:0} ruby_runtime:0
2.58 2.58 0.00 0.00 1200000/1200000 Hash#default
--------------------------------------------------------------------------------
6.63 4.10 0.00 2.53 600000/600000 Fixnum#xchr
5.69% 3.52% 6.63 4.10 0.00 2.53 600000 Range#=== (ruby_runtime:0} ruby_runtime:0
2.53 2.53 0.00 0.00 1200000/1200000 Fixnum#<=>
--------------------------------------------------------------------------------
3.72 3.72 0.00 0.00 1800000/2600000 Kernel#===
0.84 0.84 0.00 0.00 400000/2600000 Builder::XmlBase#_newline
0.84 0.84 0.00 0.00 400000/2600000 Builder::XmlBase#_indent
4.63% 4.63% 5.40 5.40 0.00 0.00 2600000 Fixnum#== (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.42 0.42 0.00 0.00 200000/600000 Enumerable#member?
2.16 1.66 0.00 0.49 100000/600000 Builder::XmlBase#method_missing
2.18 1.69 0.00 0.49 100000/600000 Builder::XmlBase#method_missing-1
0.42 0.42 0.00 0.00 200000/600000 Builder::XmlMarkup#_insert_attributes
4.44% 3.59% 5.18 4.19 0.00 0.99 600000 Array#each (ruby_runtime:0} ruby_runtime:0
0.42 0.42 0.00 0.00 200000/400000 Module#===
0.56 0.56 0.00 0.00 200000/200000 Hash#merge!
--------------------------------------------------------------------------------
2.13 1.71 0.00 0.42 200000/400000 Builder::XmlBase#method_missing
2.13 1.71 0.00 0.42 200000/400000 Builder::XmlBase#method_missing-1
3.65% 2.93% 4.26 3.42 0.00 0.84 400000 Builder::XmlBase#_indent (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:127} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:127
0.84 0.84 0.00 0.00 400000/2600000 Fixnum#==
--------------------------------------------------------------------------------
2.12 1.70 0.00 0.42 200000/400000 Builder::XmlBase#method_missing
2.12 1.70 0.00 0.42 200000/400000 Builder::XmlBase#method_missing-1
3.63% 2.91% 4.24 3.40 0.00 0.84 400000 Builder::XmlBase#_newline (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:122} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:122
0.84 0.84 0.00 0.00 400000/2600000 Fixnum#==
--------------------------------------------------------------------------------
3.43 1.56 0.00 1.87 100000/100000 Builder::XmlBase#method_missing-1
2.94% 1.34% 3.43 1.56 0.00 1.87 100000 Builder::XmlBase#_nested_structures-1 (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:132} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:132
0.22 0.22 0.00 0.00 100000/200000 Fixnum#-
1.44 1.21 0.00 0.22 100000/100000 Proc#call-1
0.21 0.21 0.00 0.00 100000/200000 Fixnum#+
--------------------------------------------------------------------------------
1.48 0.97 0.00 0.51 100000/200000 Builder::XmlBase#method_missing
1.49 0.98 0.00 0.51 100000/200000 Builder::XmlBase#method_missing-1
2.55% 1.67% 2.98 1.95 0.00 1.03 200000 Builder::XmlMarkup#_end_tag (/opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:294} /opt/local/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/xmlmarkup.rb:294
0.55 0.55 0.00 0.00 200000/600000 Symbol#to_s
0.48 0.48 0.00 0.00 200000/800000 String#<<
--------------------------------------------------------------------------------
2.58 2.58 0.00 0.00 1200000/1200000 Hash#[]
2.21% 2.21% 2.58 2.58 0.00 0.00 1200000 Hash#default (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
2.53 2.53 0.00 0.00 1200000/1200000 Range#===
2.17% 2.17% 2.53 2.53 0.00 0.00 1200000 Fixnum#<=> (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.93 0.93 0.00 0.00 400000/800000 Builder::XmlMarkup#_start_tag
0.48 0.48 0.00 0.00 200000/800000 Builder::XmlMarkup#_end_tag
0.49 0.49 0.00 0.00 200000/800000 Hash#each
1.63% 1.63% 1.90 1.90 0.00 0.00 800000 String#<< (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.56 0.56 0.00 0.00 200000/600000 Builder::XmlMarkup#_start_tag
0.55 0.55 0.00 0.00 200000/600000 Builder::XmlMarkup#_end_tag
0.54 0.54 0.00 0.00 200000/600000 Hash#each
1.41% 1.41% 1.64 1.64 0.00 0.00 600000 Symbol#to_s (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
1.56 1.56 0.00 0.00 600000/600000 Fixnum#xchr
1.34% 1.34% 1.56 1.56 0.00 0.00 600000 Integer#chr (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
1.44 1.21 0.00 0.22 100000/100000 Builder::XmlBase#_nested_structures-1
1.23% 1.04% 1.44 1.21 0.00 0.22 100000 Proc#call-1 (ruby_runtime:0} ruby_runtime:0
0.22 0.22 0.00 0.00 100000/100000 String#==
--------------------------------------------------------------------------------
1.43 1.01 0.00 0.42 200000/200000 Hash#each
1.23% 0.87% 1.43 1.01 0.00 0.42 200000 Enumerable#member? (ruby_runtime:0} ruby_runtime:0
0.42 0.42 0.00 0.00 200000/600000 Array#each
--------------------------------------------------------------------------------
1.26 1.26 0.00 0.00 600000/600000 Fixnum#xchr
1.08% 1.08% 1.26 1.26 0.00 0.00 600000 Fixnum#< (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.44 0.44 0.00 0.00 200000/400000 Builder::XmlMarkup#_attr_value
0.42 0.42 0.00 0.00 200000/400000 Array#each
0.74% 0.74% 0.86 0.86 0.00 0.00 400000 Module#=== (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.65 0.65 0.00 0.00 200000/200000 Builder::XmlBase#_escape_quote
0.56% 0.56% 0.65 0.65 0.00 0.00 200000 String#gsub (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.59 0.59 0.00 0.00 200000/200000 String#to_xs
0.50% 0.50% 0.59 0.59 0.00 0.00 200000 Array#join (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.59 0.59 0.00 0.00 200000/200000 String#to_xs
0.50% 0.50% 0.59 0.59 0.00 0.00 200000 String#unpack (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.56 0.56 0.00 0.00 200000/200000 Array#each
0.48% 0.48% 0.56 0.56 0.00 0.00 200000 Hash#merge! (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing-1
0.38% 0.38% 0.44 0.44 0.00 0.00 200000 Kernel#kind_of? (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing-1
0.38% 0.38% 0.44 0.44 0.00 0.00 200000 NilClass#nil? (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#method_missing-1
0.38% 0.38% 0.44 0.44 0.00 0.00 200000 Array#first (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.44 0.44 0.00 0.00 200000/200000 Builder::XmlMarkup#_insert_attributes
0.37% 0.37% 0.44 0.44 0.00 0.00 200000 Kernel#nil? (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.43 0.43 0.00 0.00 200000/200000 Builder::XmlMarkup#_attr_value
0.37% 0.37% 0.43 0.43 0.00 0.00 200000 String#to_s (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.21 0.21 0.00 0.00 100000/200000 Builder::XmlBase#_nested_structures
0.22 0.22 0.00 0.00 100000/200000 Builder::XmlBase#_nested_structures-1
0.37% 0.37% 0.43 0.43 0.00 0.00 200000 Fixnum#- (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.21 0.21 0.00 0.00 100000/200000 Builder::XmlBase#_nested_structures
0.21 0.21 0.00 0.00 100000/200000 Builder::XmlBase#_nested_structures-1
0.36% 0.36% 0.42 0.42 0.00 0.00 200000 Fixnum#+ (ruby_runtime:0} ruby_runtime:0
--------------------------------------------------------------------------------
0.22 0.22 0.00 0.00 100000/100000 Proc#call-1
0.19% 0.19% 0.22 0.22 0.00 0.00 100000 String#== (ruby_runtime:0} ruby_runtime:0
forewarning: anyone who goes off about builders complexity or use of method_missing being the source of, well, anyones "problems" doesn't get it, and I will treat your comments with sufficient disregard. The point is the profile difference is a design impact, not an implementation detail. And yeah, I know that looks broken with the box, i'm accepting patches, yeah
In the above example, I also went through memoize and a number of other ideas, but that turned out to be a lot slower than just building the strings as simply as possible using interpolation, for strings of this length, being used in this manner. It's probably important to note that we were building many small nodes. Oh, and I should also note that it may not be so good for very large nodes.
Also, it's the user experience that counts, so measure based on what the users are actually doing, not the slowest performing method or some such metric. You could spend all the time in the world building up lists of what operator is slower than what under what conditions or what this is faster than what that. If that's you, seriously, drop it. It's bad for you, and your code.