【问题标题】:Is using multiple for loops compared to one for loop more memory and time costly in Matlab?与一个 for 循环相比,在 Matlab 中使用多个 for 循环是否需要更多的内存和时间?
【发布时间】:2021-06-01 00:18:13
【问题描述】:

Matlab 中的哪些时间和内存效率更高?

我正在编写伪代码。

1)

for:
 Open a file
 Read the temperature info
 find the min max and avg temperature value;
for:
 Open a file
 Read the voltage info
 find the min max and avg voltage value;
for:
 Open a file
 Read the resistence info
 find the min max and avg resistence value;

或者下面的代码?

2)

for:
 Open a file
 Read the temperature info
 find the min max and avg temperature value
 Open a file
 Read the voltage info
 find the min max and avg voltage value
 Open a file
 Read the resistence info
 find the min max and avg resistence value;

【问题讨论】:

  • 这无法回答,唯一能确定需要更少时间的方法是实现两者并进行比较。但很可能“打开文件”和“从文件读取”是这里更昂贵的操作。循环本身并不昂贵,但是你在里面做的事情会重复很多次,这就是人们说循环很昂贵的原因。
  • 您真的在单独的小文件中拥有所有这些信息吗?还是您多次打开和读取同一个文件?

标签: matlab for-loop memory time memory-management


【解决方案1】:

两种实现在时间和内存方面应该没有什么区别。它们之间的唯一区别是重用循环变量,可以忽略不计。

顺便说一句,MATLAB 现在有了 JIT,所以循环本身不再昂贵(这是很多年前的事了)。

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 2020-07-22
    • 2021-11-06
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多