编译python代码时内存增加,计算机崩溃。py为330MB

2024-04-25 20:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一台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条相似


Tags: and代码selfoutputifdef计算机sys
1条回答
网友
1楼 · 发布于 2024-04-25 20:28:29

使用一长串的elif可能不是这里要做的事情。基本上,它看起来像是将一组4个整数映射为state的单个整数和output['loc']的另一个整数。您可以很容易地使用字典来实现这一点,其中键是由4个数字组成的元组,值是所需的整数。即代替

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
. . . 

就这么做吧

d = {}
d[(0, 3, 37, 1)] = (8022, 49)
d[(0, 0, 26, 2)] = (870, 47)
. . .

依此类推,对于所有不同的状态,都有elifs

一旦您创建了d,那么您的函数move就是

def move(self, sys_delta_row, env1, sys_delta_col):
    output = {}
    self.state, output['loc'] = d[(self.state, sys_delta_row, env1, sys_delta_col)]

当然,d可能是self.d,如果这对你的情况有意义的话。我不知道为什么要为output创建dict,而它似乎只包含一个值。你知道吗

相关问题 更多 >