pyeval7 icon indicating copy to clipboard operation
pyeval7 copied to clipboard

Benchmark 3 different solutions for fast deck generation

Open TommyB1992 opened this issue 3 years ago • 0 comments

My code is 50% C++ and 50% Python and I connect the first with the second troguht different hacks (es. I generate dinamically C++ code with Python and then I include it with #include, or I open the shell, call the python script and it reads the pipeline and so on).

One problem I had is to generate a great number of hands trought your library in a "acceptable" time and I want to share with you my three different solutions with relative benchmarks.

import time
import random
import numpy as np
import eval7 as e7

######################
bench = time.time()
for i in range(1000000):
    deck = e7.Deck()
    deck.shuffle()
    hands = deck.deal(4)
    board = deck.deal(5)
print("bench1:", time.time() - bench)

######################
bench = time.time()
deck = e7.Deck()
deck.shuffle()
cards = deck.deal(52)
for i in range(1000000):
    random.shuffle(cards)
    hands = cards[:4]
    board = cards[4:9]
print("bench2:", time.time() - bench)

######################
bench = time.time()
num_iters = 1000000
combos = np.random.randint(52, size=(num_iters, 9))
deck = e7.Deck()
cards = np.array(deck.deal(52))
for i in range(1000000):
    hands = cards[combos[i][:4]]
    board = cards[combos[i][4:]]
print("bench3:", time.time() - bench)

bench1: 177.55185198783875 bench2: 77.64555835723877 bench3: 18.77227473258972

Vanilla eval7 is the worst, the most "compatible" with simple edit of library code is the second one, and the last one is the most difficult to integrate and reuse it but the difference of performances has no rivals.

TommyB1992 avatar Jan 18 '23 01:01 TommyB1992