I use slightly different technique to evaluate code snippets performance. Here is how I did it in this case:
timings = Benchmark.measure do
10_000_000.times do
<code to measure goes here>
end
end
puts timings
That snippet executes 10M times code in the loop body and prints results into the console.
Funny thing though, my initial draft of the plugin that used insert
has resulted in horrible performance and terminated process (very bad sign) and the version you sketched initially reverse + reverse
were slow but by any means outperformed insert
version of it.
Only when I started to look for faster ways to combine strings in Ruby, I found out that string interpolation is actually quite performant way to do that.
Glad you find it all useful!