String Handling in Ruby

Hello all,

I am trying to inspect how ruby allocates string.

I created an array of size 61 million and allocated two different kinds
of string in that array using a loop. One small string like “MAC” and
second a big string (like from a to z). And i tried to measure the time
of the whole process using Time function.

Strangely while allocating the small string the program takes 120
seconds whereas while allocating the big string it takes 65 seconds.

Can any one tell me why this time difference is going on?

Regards

Tridib

On 1/08/2012, at 7:24 AM, Tridib B. [email protected]
wrote:

Can any one tell me why this time difference is going on?

Here is an article that explains how strings are stored…

http://patshaughnessy.net/2012/1/4/never-create-ruby-strings-longer-than-23-characters

Henry

Thanks Henry that was very much helpful.

OK now I have my own extension which allocates object. It takes 48
seconds to allocate 61 million number of strings in an array which my
extension creates whereas regular ruby takes 120 seconds to allocate
strings in a regular array. What makes the Ruby to take so long time to
allocate the string?

Thank You

Tridib

On Wed, Aug 1, 2012 at 5:02 AM, Tridib B.
[email protected] wrote:

Thanks Henry that was very much helpful.

OK now I have my own extension which allocates object. It takes 48
seconds to allocate 61 million number of strings in an array which my
extension creates whereas regular ruby takes 120 seconds to allocate
strings in a regular array. What makes the Ruby to take so long time to
allocate the string?

You did not even show your original code, let alone the modified
version. How can you expect we can answer these questions?

Cheers

robert

2012/8/1 Tridib B. [email protected]:

Thanks Henry that was very much helpful.

OK now I have my own extension which allocates object. It takes 48
seconds to allocate 61 million number of strings in an array which my
extension creates whereas regular ruby takes 120 seconds to allocate
strings in a regular array. What makes the Ruby to take so long time to
allocate the string?

We don’t know how your code looks, so we can only guess. My guess is
that there is a difference between rb_str_new() (or however you are
construncting the string) and rb_str_init (I think this is the method
used internally by Ruby to create new strings from Ruby code).

There is also the obvious overhead of Ruby method calls and closures
(something as simple as “100_000_000.times{}” takes over 10 seconds on
my machine, while the equivalent C code “for(int i=0; i<100000000;
i++){}” compiled with -O0 takes half a second).

– Matma R.