MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   python vs matlab:在2个嵌套的for循环中分配值的性能很差 (https://www.labfans.com/bbs/showthread.php?t=22228)

poster 2019-11-28 17:20

python vs matlab:在2个嵌套的for循环中分配值的性能很差
 
我真的很感谢有关python脚本的一些帮助。我在matlab中开发了一个代码,然后在python中进行了翻译。

Matlab中的这段代码需要少于2秒的时间,n = 200

x = linspace(xmin,xmax,n); y = linspace(ymin,ymax,n); for i = 1:n for j = 1:n z(i,j) = my_function(x(i),y(j)); end end 相反,在python3中超过1分钟:

x = np.linspace(xmin,xmax,n) y = np.linspace(ymin,ymax,n) for i in range(0,n): for j in range(0,n): z[i][j] = my_function(x[i],y[j]) my_function是一个简单的函数,需要2点作为输入,并经过几次检查后返回0或1。

关于如何提高python代码性能的任何建议?

提前致谢。



[url=https://stackoverflow.com/q/59080303]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 19:49

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.