Effectively Convert Java List to Matlab Matrix

I am calling the Google Protocol Buffers Java API from Matlab. This works very well, but I ran into a big lack of performance. The bulk of the data is returned as objects of the type:

java.util.Collections$UnmodifiableRandomAccessList

In fact, they contain a list of floats. I need to convert this to a Matlab matrix. The best approach I've found so far is to call:

cell2mat(cell(Q.toArray()))

However, this line is a huge bottleneck in the code.

Note. I know FarSounder Matlab parser generators for Google protocol buffers, unfortunately they are very slow. Below are some approximate test speeds for my problem (YMMV). Awesome good.

  • Harvard Matlab: 0.03
  • Pure Python: 1
  • Java API called from Matlab (only for parsing and retrieving metadata): 10
  • API Java, Matlab ( , ): 0.25

java.util.Collections$UnmodifiableRandomAccessList Matlab, Java API Matlab .

Java Matlab?

, , , .

+4
2

, java-, :

import java.util.List;
import java.util.ListIterator;

class Helper {
    public static float[] toFloatArray(List l) {
        float retValue[] = new float[l.size()];
        ListIterator iterator = l.listIterator();
        for (int idx = 0; idx < retValue.length; ++idx ){
            // List had better contain float values,
            // or else the following line will ClassCastException.
            retValue[idx] = (float) iterator.next();
        }
        return retValue;
    }
}

:

>> j = java.util.LinkedList;
>> for idx = 1:1e5, j.add(single(idx)); end
>> tic, out = Helper.toFloatArray(j); toc
Elapsed time is 0.006553 seconds.
>> tic, cell2mat(cell(j.toArray)); toc
Elapsed time is 0.305973 seconds.
+7

, java-, . matlab.

, , java.lang.Float s, :

public static float[] toFloats(Float[] floats) {
    float[] rv = new float[floats.length];
    for (int i=0; i < floats.length; i++) rv[i] = (float) floats[i];
    return rv;
}

matlab cell2mat(cell(Q.toArray())), , :

some.package.toFloats(Q.toArray());

, , , toArray() ( ?).

+1

Source: https://habr.com/ru/post/1523371/


All Articles