[英]How to make these nested for-loop more efficient?
I have two lists: 我有两个清单:
- ActiveExchange = [] ActiveExchange = []
- ActiveStock = [] ActiveStock = []
My code is very sequential in nature and I'm trying to optimize it: 我的代码本质上是非常顺序的,我正在尝试对其进行优化:
for exchange in ActiveExchange:
for stock in Activestock:
...
...
...
However, it seems to be highly inefficient due to the for-loop. 但是,由于for循环,它似乎效率很低。 What can i do to make it run more efficiently? 我该怎么做才能使其更有效地运行?
Anything else will be case-dependent. 其他任何情况都将视情况而定。 Like: 喜欢:
C/C++ and Python are have row-major arrays. C / C ++和Python具有以行为主的数组。 If you are stepping through an array, list, vector, etc, be sure to do this: 如果要遍历数组,列表,向量等,请确保执行以下操作:
for i in range(25):
for j in range(25):
A[j][i] *= 1
rather than: 而不是:
for i in range(25):
for j in range(25):
A[i][j] *= 1
That can make a difference of 3-6 times in performance. 这样可以使性能相差3-6倍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.