On Wed, Apr 27, 2011 at 12:46 PM, Ville Sipola <ville_sipola / hotmail.com> w=
rote:
> Good day
>
> I have a number of large arrays in a C module, and I'd need to move them
> to ruby as quickly as possible.
>
> Currently I use the following type of an approach:
>
> instance_array_in_ruby =3D rb_iv_get(calling_class, "@x");
> double c_array[1000000];
>
> for(i=3D0;i<1000000;i++)
> {
> =A0 rb_ary_store(instance_array_in_ruby, i, rb_float_new(c_array[i]));
> }
>
> But that's slow, apparently due to the large number of rb_float_new:s.
>
> There is a function called rb_ary_new4 that would seem to fit
> the purpose of quickly moving an entire C array to ruby. Only I'm unable
> to get it work, as it, too requires VALUE:s.
> I've tried many things including mumbo-jumbo like
> rb_iv_set(calling_class,"@x",rb_ary_new4(1000000, (VALUE)c_array));, but
> I'm only managing to get segfaults or solutions that still need a
> million rb_something_new:s.
>
> Would you know an efficient way of moving a large data from C array to
> ruby array?

What about leaving it in C, i.e. wrapping in a specific type which
presents an Array (or Enumerable) like API and uses your C array
internally?  A similar approach is taken by ENV (you can find the code
in hash.c).

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/