Some thoughts on the above code...
- If the cards() array holds numbers, it should be of numeric format (preferably Long as mentioned above). Using the Val() function all the time adds heaps of processing overhead.
- With 7 cards, couldn't you have 3 pair? (As you can see, I'm not a poker player)
- Don't a straight or a flush (or a straight flush) outrank four of a kind?
- You might save a little time by using a faster sort method. It looks as though you've coded a bubble sort. This is about the most commonly-written technique, because it's so simple and intuitive. But it's also one of the slowest available. Depending on the circumstances, of course - sorting is a complex subject. You might want to have a look at the Wikipedia article on sorting algorithms
- For reasons of readability, I'd recommending you stick with either single-line or multi-line IF...END IF syntax, don't mix them. For example, line see 46 and lines 63-66. I believe the latter is much easier to follow.
- Since you've already checked for three or four of a kind, in the pair-checking you might save a bit of time by skipping ahead one card after you detect a pair.
- Is the sequence of the checks correct? Since you're stopping as soon as your detect something, presumably you need to check in reverse order of strength so that you always detect the strongest hand present. As I said I'm not a poker player (or even much of a fan) but it doesn't look to me as though this is what happens here.
- I think the full-house (and three of a kind) check could be improved quite simply. Do the initial checking for three of a kind using the original array. If found, then if necessary copy the cards to your second array and continue checking. Given that the majority of hands (I assume) won't contain three of a kind, this would usually skip a lot of the overhead.
- If you're really looking to squeeze out every last processor cycle, there are further tweaks which may sound silly. For example, define a Const One as Long = 1 and use that everywhere in place of the literal value 1. Similarly for other commonly-used numbers. Believe it or not, it can be slightly faster to access a constant like this than a literal.
- You can't quote 15,000 hands a second then talk about once in 72,000 hands as though it's a rare thing. That's an average of about once every 4-5 seconds. Basically, in programming you usually need to take into account what is possible, not just what is likely.
- Even if you stick with the bubble sort, I think we can tweak it slightly for performance. There's no need to loop through the entire array each time 'round the While loop. Depending on which direction your values "bubble", you can start or stop a bit further along the array each time to reduce overhead.
- I think there are still areas where we can rewrite these checks to reduce processing - possibly by combining some of the checks. A bit will depend on the sequence of strengths I mentioned above, though.
- Some checks such as for pairs you might be able to incorporate into other places, such as the sort. Since you're already looping through the whole array (and the last time through the sort loop, they are in sequence) you might be able to skip one entire loop in the majority of cases, at the expense of some unnecessary checking in cases where there's a stronger hand. (In other words, if you hit a flush, you no longer care that you earlier found a pair. But this usually won't happen). The idea is that you cut down on the work which is almost always done, at the expense of some extra overhead in the rare cases.
Comment