Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3563

Re: Fast way to move C array to ruby (rb_ary_new4)?

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Fast way to move C array to ruby (rb_ary_new4)?
Date 2011-04-27 11:15 -0500
Organization Service de news de lacave.net
Message-ID <46abaf7c3f9c2e4618f00dca4c370ba6@ruby-forum.com> (permalink)
References <7bb00dec91cd520ed45220d7d899f294@ruby-forum.com>

Show all headers | View raw


Ville Sipola wrote in post #995293:
> instance_array_in_ruby = rb_iv_get(calling_class, "@x");
> double c_array[1000000];
>
> for(i=0;i<1000000;i++)
> {
>    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.

I'm afraid that's bound to be the case, given that Floats are not 
immediate values and therefore each one has to be allocated on the heap 
(unless your data happens to have many instances of identical floats, 
and could share the references)

Robert's wrapping sounds like the best way forward, perhaps with some 
memoizing (i.e. lazy creation of Float objects)

It might also help a little if you pre-allocate the array:

a = Array.new(1000000)

For more of a hack, try:

a = Array.new(1000000) { 0.0+0.0 }

Then maybe you could walk the array and overwrite the RFloat structures 
directly (ugh). Note that the 0.0+0.0 frig was required so that the same 
object_id doesn't get used in each element, and that's not guaranteed to 
work in future (e.g. if Ruby did more aggressive constant folding)

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-04-27 05:46 -0500
  Re: Fast way to move C array to ruby (rb_ary_new4)? Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 07:08 -0500
    Re: Fast way to move C array to ruby (rb_ary_new4)? Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 08:06 -0500
      Re: Fast way to move C array to ruby (rb_ary_new4)? Cameron McBride <cameron.mcbride@gmail.com> - 2011-04-27 15:02 -0500
        Re: Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-04-27 17:01 -0500
          Re: Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-05-06 14:47 -0500
  Re: Fast way to move C array to ruby (rb_ary_new4)? Brian Candler <b.candler@pobox.com> - 2011-04-27 11:15 -0500

csiph-web