我有一个循环:
for i in range(self.worldWidth):
for j in range(self.worldHeight):
originalValue = self.world.world[i, j]
# Process newValue
self.world.world[i, j] = newValue
当世界大小为(500500)时,它的运行速度约为每秒10次,这对于我正在做的事情来说很慢,当我尝试用C#做同样的事情时,我得到了三倍的速度(每秒30次)。
有什么办法可以加快速度吗
注意:这些速度的计算不需要对该值进行任何处理
编辑:在使用C#进行进一步测试后,我得到了大约10倍的速度(只是第一次速度很慢),但在python中,每次大约0.07秒
可能通过使用range关键字将
self.worldWidth
和self.worldHeight
声明为带有插入数字的列表。 这将保存每次迭代的下一个()数的计算注意-与使用循环中下一个索引计算的范围选项相比,这种方法将导致内存浪费
不知道你想做什么,但把numpy标记作为一个指标。Numpy在编译代码中运行其内部循环,因此比使用显式Python循环快得多
如果您需要计算,如果可以在numpy中定义,这些计算也可以快得多
正如一些评论所暗示的那样。这取决于生成
newValue
所需的计算。考虑到计算时间的潜在改进,可能值得为您的应用程序探索numpy相关问题 更多 >
编程相关推荐