How can I speed up the nested loops in my program?

Viewed 77

Im working on a slot machine simulator. Code

The heart of the program is a nested loop like this:

import pandas

for universe in range(10000):
    for spins in range(50000):
        win = paytable.Multiplier.sample(weights=paytable.Probability)
        result.append(win)

Universes are the amount of times the wagering process should be simulated.

Spins are the amount of spins played in each universe.

The program makes a weighted choice from a pandas dataframe to determine if a spin won and how much.

The problem is that I need to execute all these operations to get a large enough sample size and this gets really slow.

I have read some stuff about multiprocessing and vectorization but I have no idea how applicable this is and where to start.

1 Answers

You can parallelize your CPU work depending on how many cores you have. Let's say you have 8

import threading
import pandas

pool_semaphore = threading.BoundedSemaphore(value=8)

## Define a wrapper function that makes clear the passing of the argument and appends to some list 'result' that I will create later.
def awrapper(myarg):
    result.append(paytable.Multiplier.sample(weights = myarg))

threads = []
result = []

for universe in range(10000):
    for spins in range(50000):
        threads.append(threading.Thread(target=awrapper, args=(paytable.Probability,)))
        time.sleep(1)
        try:
            threads[-1].start()
            print(threading.active_count())
        except:
            time.sleep(1)
            print('oops. Error')
for t in threads:
    t.join()

Now. If the function patytable.Multiplier.sample is simple enough. You can perhaps parallelize in GPU. But that's a whole different thing.

Related