Devlog #6 Optimization of the Generating Map

I don't want to put off optimization of the game anymore. I have recently searched about threading and have some keywords like threadpool, threading, parallel programming, etc., and their connotations that remain in my mind. So I'm going to investigate these threads on this post and I will try to implement these to prototype.

The loading of the game takes a lot of time. I assume I'm able to solve this issue with threadpool. But we shouldn't move fast. Let's break the loading of the prototype into pieces. One of the important sections on the initialization of the game is using the Perlin noise to generating a smooth-random tilemap. I remember when Perlin noise was created by using three octaves were causing the loading to be slow. I found the first part. (func. "genNoise")

The other part can be generating of the chunks. I generated two chunks, one for the lower layer and one for the upper layer. Their run-time takes time obviously. This function seems complex compared to function "genNoise". It can be related to bad code.😑 Anyway, the second function is (func. "generate_chunk")

So, the last function is about generating the map data. (func. "generate_map"). The common of these functions is nested-loop. So, we have to go on this.

But first, it would be more accurate if we read these articles and threads:
I looked at my system that how many cores in it with Task Manager. (ctrl+shift+esc). We can see cores and logical processors exactly. But we can learn how many processors in our system with Python.
task manager
These conditions are optional from system to system. Therefore, we can optimize the system itself. There are too many options for this in Python. 

I'm going to use multiprocessing module. We can learn more things about it from this official document. Also, I will measure the time of the execution of the processor with the time module.

I typed an imitation code from the original my project. This function (func. "example_generate") ends in 5 seconds, exactly as expected:
import time
start = time.perf_counter()

def genNoise(i,j):

def example_generate():
    for i in range(650):
        for j in range(650):
            genNoise(i, j)

start = time.perf_counter()


finish = time.perf_counter()

print("Time of execution {0} seconds".format(round(finish-start, 3)))
So, I decided to use itertools which is a python module, to create the necessary parameters (i,j) for the noise function. thus I could use with pool which is a feature of multiprocessing easily. Finally, I can implement the multiprocessing usage to generate_map function. In the end, I compared to new method with the old method according to the time of execution. After the test results, everything seems fine:
the generating of map
This results by the test, we achieved 66% efficiency approximately. Let's implement this new function to the prototype. So, I did but there is awkward output when I applied to multiprocessing operation on the prototype, like this:
Multiprocessing pygame
Maybe, It's not a good idea to use multiprocessing with pygame. BTW, The prototype works, and generating operation has happened earlier. But I canceled on it. I have to study multithreading in Python. These things didn't go as I thought. But in the meantime, I have learned useful information. Also, It is good study for the loading screen on prototype, for the next devlog.

No comments:

Post a Comment