From 8143ed6e77af510c293ac6b1454b224842b24e68 Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Mon, 24 Jul 2023 22:10:07 -0700 Subject: [PATCH 1/2] Avoid broadcast, add benchmark script --- perf/benchmark.jl | 25 +++++++++++++++ src/best_hand_rank_from_n_cards.jl | 50 ++++++++++++++++-------------- src/evaluate5.jl | 10 +++--- 3 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 perf/benchmark.jl diff --git a/perf/benchmark.jl b/perf/benchmark.jl new file mode 100644 index 0000000..cb576d7 --- /dev/null +++ b/perf/benchmark.jl @@ -0,0 +1,25 @@ +using PlayingCards +using PlayingCards: AbstractDeck +import Random +Random.seed!(1234) +import PokerHandEvaluator as PHE + +struct DummyDeck <: AbstractDeck + cards::Vector{Card} +end +Base.pop!(deck::DummyDeck, n::Int) = ntuple(i->deck.cards[i], n) +Random.shuffle!(deck::DummyDeck) = Random.shuffle!(Random.default_rng(), deck) + +function Random.shuffle!(rng::Random.AbstractRNG, deck::DummyDeck) + Random.shuffle!(rng, deck.cards) + return deck +end + +function do_work!(deck) + fhe = PHE.FullHandEval(pop!(deck, 7)) + shuffle!(deck) +end + +deck = DummyDeck(full_deck()) +using BenchmarkTools +@benchmark do_work!($deck) diff --git a/src/best_hand_rank_from_n_cards.jl b/src/best_hand_rank_from_n_cards.jl index ec535dd..f23a74f 100644 --- a/src/best_hand_rank_from_n_cards.jl +++ b/src/best_hand_rank_from_n_cards.jl @@ -22,32 +22,34 @@ function min_hand_rank_compact(hand_rank_1, cards) end function best_hand_rank_from_7_cards_full(c) - cards=(c[1],c[2],c[3],c[4],c[5]) # first combination + @inbounds begin + cards=(c[1],c[2],c[3],c[4],c[5]) # first combination - hand_rank = evaluate5(cards) - hand_type = hand_type_binary_search(hand_rank) - hr = (hand_rank, hand_type, cards) + hand_rank = evaluate5(cards) + hand_type = hand_type_binary_search(hand_rank) + hr = (hand_rank, hand_type, cards) - cards=(c[1],c[2],c[3],c[4],c[6]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[3],c[4],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[3],c[5],c[6]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[3],c[5],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[3],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[2],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[3],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[3],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[3],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[3],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[1],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[2],c[3],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) - cards=(c[2],c[3],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[2],c[3],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[2],c[3],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[2],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) - cards=(c[3],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[3],c[4],c[6]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[3],c[4],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[3],c[5],c[6]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[3],c[5],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[3],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[2],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[3],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[3],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[3],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[3],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[1],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[2],c[3],c[4],c[5],c[6]); hr = min_hand_rank_full(hr, cards) + cards=(c[2],c[3],c[4],c[5],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[2],c[3],c[4],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[2],c[3],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[2],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + cards=(c[3],c[4],c[5],c[6],c[7]); hr = min_hand_rank_full(hr, cards) + end return hr end diff --git a/src/evaluate5.jl b/src/evaluate5.jl index 8844e47..380407b 100644 --- a/src/evaluate5.jl +++ b/src/evaluate5.jl @@ -28,10 +28,12 @@ evaluate5(cards::Card...)::Int = evaluate5(cards) # based on flush/non-flush: function evaluate5(t::NTuple{N,Card})::Int where {N} @assert N == 5 - if suit(t[1]) == suit(t[2]) == suit(t[3]) == suit(t[4]) == suit(t[5]) - return hash_table_suited[prod(prime.(t))] - else - return hash_table_offsuit[prod(prime.(t))] + @inbounds begin + if suit(t[1]) == suit(t[2]) == suit(t[3]) == suit(t[4]) == suit(t[5]) + return hash_table_suited[prod(prime, t)] + else + return hash_table_offsuit[prod(prime, t)] + end end end From ebba886199105c313673e0996a71b2f56fcc3ddc Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Mon, 24 Jul 2023 23:04:58 -0700 Subject: [PATCH 2/2] Bump patch version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 82d50e0..0b26f45 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PokerHandEvaluator" uuid = "18ed25b1-892a-4a3b-b8fc-1036dc9a6a89" authors = ["Charles Kawczynski "] -version = "0.2.4" +version = "0.2.5" [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"