简体   繁体   English

优化 dict 生成的嵌套 for 循环 Python 3

[英]Optimize nested for loop for dict generation Python 3

I am developing a game with an infinite world.我正在开发一个无限世界的游戏。 I have this function to generate chunks of it whenever I need them, and I just need to pass it the chunk coordinates and it will create and store a chunk inside a dictionary.我有这个 function 可以在需要时生成它的块,我只需要将块坐标传递给它,它就会在字典中创建和存储一个块。 This is the function:这是 function:

def generate(self, chunkx, chunky):

    # print("Generating chunk at", chunkx, chunky)


    Z_INDEX = [0,1,2,3]

    floor_void_diff = 60
    mountain = floor_void_diff + 7
    high_mountain = mountain + 6

    factor = 1/200
    biome_factor = 1/300

    floor = {}
    items = {}
    chunk = (chunkx, chunky)

    if chunk not in self.chunks:

        # print("Generating chunk at {}".format(chunk))
        for y in range(chunky * core["world_gen"]["chunksize"], chunky * core["world_gen"]["chunksize"] + core["world_gen"]["chunksize"]):
            for x in range(chunkx * core["world_gen"]["chunksize"], chunkx * core["world_gen"]["chunksize"] + core["world_gen"]["chunksize"]):

                temperature = create_noise(8, x, y, 0.5, biome_factor, 0, 100, self.n1)
                humidity = create_noise(8, x, y, 0.5, biome_factor, 0, 100, self.n2)
                altitude = create_noise(8, x, y, 0.5, biome_factor, 0, 100, self.n3)

                climate = (temperature, altitude, humidity)

                # gen_biome(climate)

                i = create_noise(16, x, y, 0.5, factor, 0, 100, self.noise)
                if i >= floor_void_diff:
                    if i >= mountain:
                        if i >= high_mountain:
                            floor.update({(x, y): {"block" : Z_INDEX[3], "climate": climate}})
                        else:
                            floor.update({(x, y): {"block" : Z_INDEX[2], "climate": climate}})
                    else:
                        floor.update({(x, y): {"block" : Z_INDEX[1], "climate": climate}})

                    spawner = random.randint(-1, core["world_gen"]["item_spawn"])
                    random_item = random.randint(0, len(core["item_list"])-1)

                    if spawner == 0:
                        items.update({(x, y): core["item_list"][random_item]})

                elif i < floor_void_diff:
                    floor.update({(x, y): {"block" : Z_INDEX[0], "climate": climate}})
                else:
                    floor.update({(x, y): EMPTY})

        self.chunks.update({chunk: {"floor": floor, "items": items}})
        self.unsaved += 1

this creates something like this:这会产生这样的东西:

self.chunks = {
(0,0): {
    "floor": {
        "block": 3,
        "climate": (23, 15, 87)
        },
    "items": {
        (2, 3): "rock"
        }
    }
}

to create it chunk the game takes up to half a second, which is not ideal.创建它块游戏需要半秒钟,这并不理想。 Is there any way I can optimize this to make it run faster?有什么办法可以优化它以使其运行得更快? Something like a numpy function or a way to thread this and make it run in the background?像 numpy function 之类的东西,还是一种线程化并使其在后台运行的方法?

You could take a look at Numba package.你可以看看 Numba package。 It is an JIT compiler, which converts Python code to machine code using LLVM compiler.它是一个 JIT 编译器,它使用 LLVM 编译器将 Python 代码转换为机器代码。 Just use @jit decorator before your loop and it might help.只需在循环之前使用 @jit 装饰器,它可能会有所帮助。 http://numba.pydata.org http://numba.pydata.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM