Skip to content

Commit

Permalink
tuneup sampling in C++(with classic functions) #59
Browse files Browse the repository at this point in the history
*tuneup sampling in C++

*revert

*replace for loop with iota
  • Loading branch information
azriel1rf committed Oct 21, 2021
1 parent 461cd14 commit 18e5bf9
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions cpp/benchmark/benchmark.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <chrono>
#include <random>
Expand All @@ -10,11 +11,11 @@ using namespace phevaluator;

static void EvaluateAllFiveCards(benchmark::State& state) {
for (auto _ : state) {
for(int a = 0; a < 48; a++) {
for(int b = a + 1; b < 49; b++) {
for(int c = b + 1; c < 50; c++) {
for(int d = c + 1; d < 51; d++) {
for(int e = d + 1; e < 52; e++) {
for (int a = 0; a < 48; a++) {
for (int b = a + 1; b < 49; b++) {
for (int c = b + 1; c < 50; c++) {
for (int d = c + 1; d < 51; d++) {
for (int e = d + 1; e < 52; e++) {
EvaluateCards(a, b, c, d, e);
}
}
Expand All @@ -27,12 +28,12 @@ BENCHMARK(EvaluateAllFiveCards);

static void EvaluateAllSixCards(benchmark::State& state) {
for (auto _ : state) {
for(int a = 0; a < 47; a++) {
for(int b = a + 1; b < 48; b++) {
for(int c = b + 1; c < 49; c++) {
for(int d = c + 1; d < 50; d++) {
for(int e = d + 1; e < 51; e++) {
for(int f = e + 1; f < 52; f++) {
for (int a = 0; a < 47; a++) {
for (int b = a + 1; b < 48; b++) {
for (int c = b + 1; c < 49; c++) {
for (int d = c + 1; d < 50; d++) {
for (int e = d + 1; e < 51; e++) {
for (int f = e + 1; f < 52; f++) {
EvaluateCards(a, b, c, d, e, f);
}
}
Expand All @@ -46,13 +47,13 @@ BENCHMARK(EvaluateAllSixCards);

static void EvaluateAllSevenCards(benchmark::State& state) {
for (auto _ : state) {
for(int a = 0; a < 46; a++) {
for(int b = a + 1; b < 47; b++) {
for(int c = b + 1; c < 48; c++) {
for(int d = c + 1; d < 49; d++) {
for(int e = d + 1; e < 50; e++) {
for(int f = e + 1; f < 51; f++) {
for(int g = f + 1; g < 52; g++) {
for (int a = 0; a < 46; a++) {
for (int b = a + 1; b < 47; b++) {
for (int c = b + 1; c < 48; c++) {
for (int d = c + 1; d < 49; d++) {
for (int e = d + 1; e < 50; e++) {
for (int f = e + 1; f < 51; f++) {
for (int g = f + 1; g < 52; g++) {
EvaluateCards(a, b, c, d, e, f, g);
}
}
Expand All @@ -67,30 +68,34 @@ BENCHMARK(EvaluateAllSevenCards);

static unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
static std::default_random_engine generator(seed);
static std::uniform_int_distribution<int> distribution(0, 51);

static std::vector<int> RandomCardSample(int size) {
std::vector<int> ret;
std::set<int> sample;

while (sample.size() < size) {
int number = distribution(generator);
if (sample.find(number) == sample.end()) {
ret.push_back(number);
sample.insert(number);
class CardSampler {
std::array<int, 52> deck;
public:
CardSampler(void) {
std::iota(deck.begin(), deck.end(), 0);
}
std::vector<int> sample(int size) {
std::vector<int> ret;
int residual_cards = deck.size();
for (int i = 0; i < size; i++) {
int target_index = generator() % residual_cards;
int tail_index = residual_cards - 1;
std::swap(deck[target_index], deck[tail_index]);
ret.push_back(deck[tail_index]);
residual_cards--;
}
return ret;
}

return ret;
}
};

const int SIZE = 100;

static void EvaluateRandomFiveCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;

CardSampler cs{};
for (int i = 0; i < SIZE; i++) {
hands.push_back(RandomCardSample(5));
hands.push_back(cs.sample(5));
}
for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
Expand All @@ -106,9 +111,10 @@ BENCHMARK(EvaluateRandomFiveCards);

static void EvaluateRandomSixCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(RandomCardSample(6));
hands.push_back(cs.sample(6));
}

for (auto _ : state) {
Expand All @@ -126,9 +132,10 @@ BENCHMARK(EvaluateRandomSixCards);

static void EvaluateRandomSevenCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(RandomCardSample(7));
hands.push_back(cs.sample(7));
}

for (auto _ : state) {
Expand All @@ -147,9 +154,10 @@ BENCHMARK(EvaluateRandomSevenCards);

static void EvaluateRandomOmahaCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(RandomCardSample(9));
hands.push_back(cs.sample(9));
}

for (auto _ : state) {
Expand All @@ -164,7 +172,8 @@ static void EvaluateRandomOmahaCards(benchmark::State& state) {
hands[i][7],
hands[i][8]);
}
}}
}
}
BENCHMARK(EvaluateRandomOmahaCards);

BENCHMARK_MAIN();
Expand Down

0 comments on commit 18e5bf9

Please sign in to comment.