Interesting.
A common rule of Ruby performance is that if a program uses built-in operations, it will be fast. If it is not, it will be slow.
Although your second version may or may not do less conversion, it does three lines of Ruby vs one.
I once read a large log file. In my first version, I kept an efficient linked list of strings using Ruby code.
1. The complexity of time: O (1).
Then I changed it to just use << and bind each new node to the end of the array.
2. Time complexity: O (N 2 ) or at least O (N 1 + ε )
The second version (1) was faster.
1. Obviously, the implementation expanded the array in pieces, so it was not completely quadratic, but still worked much more than a linked list.
Digitaloss
source share