[ruby-ext:02330] 質問:Cの配列をrubyの配列に効率よく変換する方法

View: New views
1 Messages — Rating Filter:   Alert me  

[ruby-ext:02330] 質問:Cの配列をrubyの配列に効率よく変換する方法

by Ichitaro Masuda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

増田です。

Cの大きなfloat配列をrubyのArrayに高速に変換したいと思っているのですが、何かよい方法はありますか?
今のところ以下のように、rb_ary_storeを利用しているのですが、もっと効率のいい方法があるのではないかと思いまして・・

VALUE wrap_ofSoundGetSpectrum(VALUE self, VALUE nBands)
{
        VALUE result;
        int len;
        int i;
        float *spec;
       
        len = NUM2INT(nBands);
        spec = ofSoundGetSpectrum(len);
       
        result = rb_ary_new2(len);
        for (i = 0; i < len; i++) {
                rb_ary_store(result, i, rb_float_new(spec[i]));
        }
       
        return result;
}

また、見よう見まねで以下も試してみたのですが、これだとBus Errorが出てしまいました・・
間抜けな質問ですみませんが、何卒よろしくお願い致します。

VALUE wrap_ofSoundGetSpectrum(VALUE self, VALUE nBands)
{
        VALUE result;
        VALUE *values
        int len;
        int i;
        float *spec;
       
        len = NUM2INT(nBands);
        spec = ofSoundGetSpectrum(len);
       
        values = ALLOC_N(VALUE, len);
        for (i = 0; i < len; i++) {
                values[i] = rb_float_new(spec[i]);
        }
        result = rb_ary_new4(len, values);
        free(values);
       
        return result;
}