[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/poker 423994e 07/12: Improve performance of poker-hand-
From: |
Stefan Monnier |
Subject: |
[elpa] externals/poker 423994e 07/12: Improve performance of poker-hand-value by a factor of 4 |
Date: |
Tue, 1 Dec 2020 16:46:50 -0500 (EST) |
branch: externals/poker
commit 423994e65b7cd173b201cd20116ed6a969d3068d
Author: Mario Lang <mlang@delysid.org>
Commit: Mario Lang <mlang@delysid.org>
Improve performance of poker-hand-value by a factor of 4
`cl-count' is unnecessarily expensive, as it at least uses `length' and
`nthcdr'
which we really don't need in this performance cricital code path.
Rewriting it without `cl-count' turns up another opportunity to speed up,
as we actually don't need to check the whole list to count occurances of
unique elements. For one, we can start counting from 1 (not 0) if
we encounter the first element, and we only need to check the rest of the
list of cards. Also, stop using `mapcar' with `poker-card-rank'
to allow it to actually be inlined. This turns out to make poker-hand-value
*a lot* faster. Mission accomplished.
---
poker.el | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/poker.el b/poker.el
index 61888ae..d2896e7 100644
--- a/poker.el
+++ b/poker.el
@@ -93,15 +93,25 @@ RANK is one of `poker-ranks' and SUIT is one of
`poker-suits'."
The result is a 24 bit integer where the leftmost 4 bits (0-8) indicate the
type
of hand, and the remaining nibbles are rank values of decisive cards.
The highest possible value is therefore #x8CBA98 and the lowest is #x053210."
- (let* ((ranks (mapcar #'poker-card-rank hand))
- (rank-counts (sort (mapcar (lambda (rank) (cons (cl-count rank ranks)
rank))
- (cl-remove-duplicates ranks))
+ (let* ((rank-counts (sort (let ((cards hand) result)
+ (while cards
+ (let ((rank (poker-card-rank (car cards))))
+ (unless (rassq rank result)
+ (push (cons (let ((count 1))
+ (dolist (card (cdr cards)
count)
+ (when (eq (poker-card-rank
card)
+ rank)
+ (setq count (1+ count)))))
+ rank)
+ result)))
+ (setq cards (cdr cards)))
+ result)
(lambda (lhs rhs) (or (> (car lhs) (car rhs))
(and (= (car lhs) (car rhs))
(> (cdr lhs) (cdr
rhs)))))))
- (ranks-length (length rank-counts)))
- (setq ranks (mapcar #'cdr rank-counts)
- rank-counts (mapcar #'car rank-counts))
+ (ranks-length (length rank-counts))
+ (ranks (mapcar #'cdr rank-counts)))
+ (setq rank-counts (mapcar #'car rank-counts))
(logior (cond
((eq ranks-length 4) #x100000)
((eq ranks-length 5)
- [elpa] branch externals/poker created (now 30f3985), Stefan Monnier, 2020/12/01
- [elpa] externals/poker 725cbdf 01/12: Add new simple package poker.el., Stefan Monnier, 2020/12/01
- [elpa] externals/poker 937e353 02/12: [poker] Add to the games menu., Stefan Monnier, 2020/12/01
- [elpa] externals/poker 2e12d6b 03/12: * poker/poker.el (poker-combinations, poker-possible-hands): Fix tests., Stefan Monnier, 2020/12/01
- [elpa] externals/poker 7d978b1 05/12: Very slightly improve performance, Stefan Monnier, 2020/12/01
- [elpa] externals/poker b9debcb 04/12: Fix some quoting problems in doc strings, Stefan Monnier, 2020/12/01
- [elpa] externals/poker fcdb916 08/12: Add a pre-flop hand strength table and an ert test for poker-hand-value, Stefan Monnier, 2020/12/01
- [elpa] externals/poker 37e54e3 06/12: Improve poker-hand-value performance by 25%, Stefan Monnier, 2020/12/01
- [elpa] externals/poker 56469f2 10/12: [poker] Version 0.2, update copyright years and add todo, Stefan Monnier, 2020/12/01
- [elpa] externals/poker 30f3985 12/12: * .gitignore: New file, Stefan Monnier, 2020/12/01
- [elpa] externals/poker 423994e 07/12: Improve performance of poker-hand-value by a factor of 4,
Stefan Monnier <=
- [elpa] externals/poker 351b253 11/12: Fix typo in poker-describe-hand (Bug#34241), Stefan Monnier, 2020/12/01
- [elpa] externals/poker fe8a532 09/12: Set default number of iterations to 300, Stefan Monnier, 2020/12/01