Profiling learns you…
This is sitting at the top of a pile of tests I wrote today in search of speeding up some areas of a program generating relatively short strings frequently.
[source:ruby]
##
# Already learned:
# * memoization is no good for short strings, even when using symbols to lookup data.
# * String#% is very slow
# * Array#to_s is faster than Array#join, for strings of this nature
# * Interpolation is faster than either of these methods
# * Frozen strings are no aid over interpolation
# * Symbol#to_s is slower than strings, for making strings
# * ? … : … is actually faster than if … else … end, (tested on 1.8.5 win32)
# (warning, reason for this was not the operation itself, and
# using if … then … else … end on one line changed that)!
# * return … is slower than not using it
#
# From #ruby-lang:
#
# 02:38 flgr: interestingly, Array#empty? can be implemented as ary.inject(true) { false }
# 02:39 flgr: what this means is that you can replace ary.empty?() ? a : b with
# ary.inject(a){ b } all the time
# 02:50 flgr: raggi: you can probably make it faster by using { break b } :)
#
# 02:56 LoganCapaldo: well you can write a.empty? ? b : a[0] as a.fetch(0, b) too
# 02:56 LoganCapaldo: for the case that the thingy you want is an element in the array
# 02:56 LoganCapaldo: or maybe that ony works with hashes
# 02:57 LoganCapaldo: nope it works with arrays too
##
[/source]