Skip to content

Commit

Permalink
Make the new/delete benchmark more real-world by defining many classe…
Browse files Browse the repository at this point in the history
…s instead of just allocating the single class many times.
  • Loading branch information
poletti-marco committed Nov 16, 2014
1 parent 4a0072c commit eef01ad
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions examples/benchmark/new_delete_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,89 @@
#include <iostream>
#include <iomanip>

using namespace std;
#define MULTIPLIER 1000

constexpr size_t num_allocations = 100;
#if MULTIPLIER == 1
#define REPEAT(X) REPEAT_1(X, _)

struct I {
virtual ~I() = default;
};
#elif MULTIPLIER == 10
#define REPEAT(X) REPEAT_10(X, _)

#elif MULTIPLIER == 100
#define REPEAT(X) REPEAT_100(X, _)

#elif MULTIPLIER == 1000
#define REPEAT(X) REPEAT_1000(X, _)

#else
#error Multiplier not supported.
#endif

#define PLACEHOLDER

#define EVAL0(...) __VA_ARGS__
#define EVAL1(...) EVAL0(EVAL0(EVAL0(EVAL0(__VA_ARGS__))))
#define EVAL2(...) EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
#define EVAL(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))

#define META_REPEAT_10(R, X, I) \
R PLACEHOLDER(X, I##0) \
R PLACEHOLDER(X, I##1) \
R PLACEHOLDER(X, I##2) \
R PLACEHOLDER(X, I##3) \
R PLACEHOLDER(X, I##4) \
R PLACEHOLDER(X, I##5) \
R PLACEHOLDER(X, I##6) \
R PLACEHOLDER(X, I##7) \
R PLACEHOLDER(X, I##8) \
R PLACEHOLDER(X, I##9)

#define REPEAT_1(X, I) \
X(I)

#define REPEAT_10(X, I) \
META_REPEAT_10(REPEAT_1, X, I)

#define REPEAT_100(X, I) \
META_REPEAT_10(REPEAT_10, X, I)

struct C : public I {
virtual ~C() = default;
#define REPEAT_1000(X, I) \
META_REPEAT_10(REPEAT_100, X, I)

using namespace std;

#define DEFINITIONS(N) \
struct I##N { \
virtual ~I##N() = default; \
}; \
\
struct C##N : public I##N { \
virtual ~C##N() = default; \
};

#define ALLOCATE(N) \
C##N* c##N = new C##N();

#define DEALLOCATE(N) \
delete c##N;

EVAL(REPEAT(DEFINITIONS))

int main() {
size_t num_loops;
cin >> num_loops;

//size_t newTime = 0;
//size_t deleteTime = 0;

clock_t start_time;

vector<C*> v(num_allocations, nullptr);

start_time = clock();

for (size_t i = 0; i < num_loops; i++) {
for (size_t j = 0; j < num_allocations; ++j) {
v[j] = new C();
}
for (size_t j = num_allocations; j > 0; --j) {
delete v[j-1];
}
EVAL(REPEAT(ALLOCATE))
EVAL(REPEAT(DEALLOCATE))
}
size_t totalTime = clock() - start_time;

std::cout << std::fixed;
std::cout << std::setprecision(2);
//std::cout << "Time for new = " << newTime * 1.0 / num_loops << std::endl;
//std::cout << "Time for delete = " << deleteTime * 1.0 / num_loops << std::endl;
std::cout << "Total = " << totalTime * 1.0 / num_loops << std::endl;

return 0;
Expand Down

0 comments on commit eef01ad

Please sign in to comment.