我有一台16gbram的计算机,带有8cpu的inter-XEON。 当编译一个330mb的python代码来表示一个自动机(一个基本上只有一个函数但有很多if-else的类)时,计算机崩溃了。RAM内存仍在增加,直到计算机崩溃。你知道吗
PS:python代码大约有9000行代码,使用文本编辑器很容易打开。你知道吗
有没有办法在编译类之前将其拆分?或者让python分头做?你知道吗
class FollowCtrl(object):
def __init__(self):
self.state = 11900
self.input_vars = ['sys_delta_row', 'env1', 'sys_delta_col']
def move(self, sys_delta_row, env1, sys_delta_col):
"""Given inputs, take move and return outputs.
@rtype: dict
@return: dictionary with keys of the output variable names:
['loc']
"""
output = dict()
if self.state == 0:
if (sys_delta_row == 3) and (env1 == 37) and (sys_delta_col == 1):
self.state = 8022
output["loc"] = 49
elif (sys_delta_row == 0) and (env1 == 26) and (sys_delta_col == 2):
self.state = 870
output["loc"] = 47
elif (sys_delta_row == 1) and (env1 == 46) and (sys_delta_col == 3):
self.state = 10585
output["loc"] = 68
elif (sys_delta_row == 1) and (env1 == 46) and (sys_delta_col == 1):
self.state = 10586
output["loc"] = 68
elif (sys_delta_row == 1) and (env1 == 26) and (sys_delta_col == 1):
self.state = 861
output["loc"] = 47
elif (sys_delta_row == 4) and (env1 == 36) and (sys_delta_col == 3):
self.state = 905
output["loc"] = 58
elif (sys_delta_row == 1) and (env1 == 46) and (sys_delta_col == 2):
self.state = 10591
这是第一条线的50条,与其他线的80 000条相似
使用一长串的
elif
可能不是这里要做的事情。基本上,它看起来像是将一组4个整数映射为state的单个整数和output['loc']
的另一个整数。您可以很容易地使用字典来实现这一点,其中键是由4个数字组成的元组,值是所需的整数。即代替就这么做吧
依此类推,对于所有不同的状态,都有
elif
s一旦您创建了
d
,那么您的函数move
就是当然,
d
可能是self.d
,如果这对你的情况有意义的话。我不知道为什么要为output
创建dict,而它似乎只包含一个值。你知道吗相关问题 更多 >
编程相关推荐