Improve performance of converting numpy array to MATLAB double

后端 未结 3 964
说谎
说谎 2020-12-06 11:52

Calling MATLAB from Python is bound to give some performance reduction that I could avoid by rewriting (a lot of) code in Python. However, this isn\'t a realistic option for

3条回答
  •  盖世英雄少女心
    2020-12-06 12:20

    While awaiting better suggestions, I'll post the best trick I've come up with so far. It comes down to saving the file with `scipy.io.savemat´ and then loading this file in MATLAB.

    This is not the prettiest hack and it requires some care to ensure different processes relying on the same script don't end up writing and loading each other's .mat files, but the performance gain is worth it for me.

    As a test case I wrote two simple, almost identical MATLAB functions that require 2 numpy arrays (I tested with length 1000000) and one int as input.

    function d = test(x, y, fs_signal)
    d = sum((x + y))./double(fs_signal);
    
    function d = test2(path)
    load(path)
    d = sum((x + y))./double(fs_signal);
    

    The function test requires conversion, while test2 requires saving.

    Testing test: Converting the two numpy arrays takes cirka 40 s on my system. The total time to prepare for and run test comes down to 170 s

    Testing test2: Saving the arrays and int takes cirka 0.35 s on my system. Suprisingly, loading the .mat file in MATLAB is extremely efficient (or more suprisingly, it is extremely ineffcient at dealing with its doubles)... The total time to prepare for and run test2 comes down to 0.38 s

    That's a performance gain of almost 450x...

提交回复
热议问题