I would like to know if there is a more efficient way to speed up below code. This function is meant to fill in a set of poker hand with the remaining cards using Mersenne Twister for a Monte Carlo simulation.
void HandEvaluator::RandomFill(std::vector<std::shared_ptr<Card>>& _Set, std::vector<std::shared_ptr<Card>>& _Dead, unsigned int _Target){
//Add the cards that are currently in Set as dead cards
for (auto const& CardInSet : _Set)
{
if (CardInSet == nullptr)
break;
_Dead.push_back(CardInSet);
}
bool IsDead;
unsigned int RequiredAmt = _Target - _Set.size();
unsigned int CardIndex = 0;
std::uniform_int_distribution<unsigned int> CardsDistribution(0, 51);
for (unsigned int Index = 0; Index < RequiredAmt; Index++)
{
while (true)
{
IsDead = false;
CardIndex = CardsDistribution(MTGenerator);
for (auto const& Dead : _Dead)
{
if (ReferenceDeck[CardIndex]->GetRank() == Dead->GetRank() && ReferenceDeck[CardIndex]->GetSuit() == Dead->GetSuit())
{
IsDead = true;
break;
}
}
if (!IsDead)
{
_Set.push_back(ReferenceDeck[CardIndex]);
break;
}
}
}
}
The Visual Studio Profiler had identified that this line
CardIndex = CardsDistribution(MTGenerator);
is the main culprit behind the high compute time. Is Mersenne Twister itself not meant for a Monte Carlo Simulation and another PRNG should be used instead ? Or there are some inefficient lines that I had missed out ?
Card
is (I can guess that it is either an integer or pair which represents a card) and what isReferenceDeck
(vector of cards?). \$\endgroup\$ – Incomputable Dec 1 '18 at 13:13_Target
amt of cards, based on the remaining cards in a deck. The generation of ReferenceDeck is in another part of theHandEvaluator
class andCard
is a class that contains theSuit
andRank
with functions to compare with otherCard
s. Here's the link to the sample project: drive.google.com/file/d/1U3d8ZuS_wzkq4RVIj2RV6gj1zyv_yrco/… \$\endgroup\$ – Tomashiwa Dec 1 '18 at 13:52