Improve performance of converting numpy array to MATLAB double

后端 未结 3 965
说谎
说谎 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:14

    My situation was a bit different (python script called from matlab) but for me converting the ndarray into an array.array massively speed up the process. Basically it is very similar to Alexandre Chabot solution but without the need to alter any files:

    #untested i.e. only deducted from my "matlab calls python" situation
    import numpy
    import array
    
    data1 = numpy.random.uniform(low = 0.0, high = 30000.0, size = (1000000,))
    ar = array.array('d',data1.flatten('F').tolist())
    p = matlab.double(ar)
    C = matlab.reshape(p,data1.shape) #this part I am definitely not sure about if it will work like that
    

    At least if done from Matlab the combination of "array.array" and "double" is relative fast. Tested with Matlab 2016b + python 3.5.4 64bit.

提交回复
热议问题