From b4bdb8bedec6002e0f7a49e8070efefaf4aecd71 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 13 Feb 2021 22:57:01 +0100 Subject: Added benchmark setup and plot script. --- benchmarks/vector_vs_deque.cpp | 355 ----------------------------------------- 1 file changed, 355 deletions(-) delete mode 100644 benchmarks/vector_vs_deque.cpp (limited to 'benchmarks/vector_vs_deque.cpp') diff --git a/benchmarks/vector_vs_deque.cpp b/benchmarks/vector_vs_deque.cpp deleted file mode 100644 index 46da98b7..00000000 --- a/benchmarks/vector_vs_deque.cpp +++ /dev/null @@ -1,355 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef struct {const char* first; int second;} Si; -using_cvec(si, Si, c_no_compare); -cvec_si tm = cvec_si_init(); - -void add(cvec_si* tm, const char* s, int n) { Si si = {s, n}; cvec_si_push_back(tm, si); } - - -void test_vector(const int num_iterations) -{ - std::vector v; - stc64_t rng = stc64_init(0); - v.reserve(num_iterations + 2); //Ensure there is enough space reserved. - - // == PUSH_BACK - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - v.push_back(stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "vector::push_back", t2 - t1); - printf("#"); - } - - // == INSERT FRONT - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - // Rather add some more elements to back. - v.push_back(stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "vector::push_back", t2 - t1); - printf("#"); - } - - // == SORT - {/* - clock_t t1 = std::clock(); - - std::sort(v.begin(), v.end()); - - clock_t t2 = std::clock(); - add(&tm, "vector::sort", t2 - t1); - printf("#");*/ - } - - // == ITERATE - { - clock_t t1 = std::clock(); - - for (int i=0; i>1); ++i) - { - number_sink += v.back(); - v.pop_back(); - } - clock_t t2 = std::clock(); - add(&tm, "vector::pop_back", t2 - t1); - printf("#"); - } -} - -void test_deque(const int num_iterations) -{ - std::deque d; - stc64_t rng = stc64_init(0); - - // == PUSH_BACK - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - d.push_back(stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "deque::push_back", t2 - t1); - printf("#"); - } - - // == INSERT FRONT - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - d.push_front(stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "deque::push_front", t2 - t1); - printf("#"); - } - - // == SORT - {/* - clock_t t1 = std::clock(); - - std::sort(d.begin(), d.end()); - - clock_t t2 = std::clock(); - add(&tm, "deque::sort", t2 - t1); - printf("#");*/ - } - - // == ITERATE - { - clock_t t1 = std::clock(); - - for (int i=0; i>1); ++i) - { - number_sink += d.back(); - d.pop_back(); - } - - clock_t t2 = std::clock(); - add(&tm, "deque::pop_back", t2 - t1); - printf("#"); - } -} - - - - -using_cvec(i, int); - - -void test_cvec(const int num_iterations) -{ - cvec_i v = cvec_i_init(); - stc64_t rng = stc64_init(0); - //v.reserve(num_iterations + 2); //Ensure there is enough space reserved. - - // == PUSH_BACK - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - cvec_i_push_back(&v, stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "cvec_push_back", t2 - t1); - printf("#"); - } - - // == INSERT FRONT - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - // Rather add some more elements to back. - cvec_i_push_back(&v, stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "cvec_push_front", t2 - t1); - printf("#"); - } - - // == SORT - {/* - clock_t t1 = std::clock(); - - cvec_i_sort(&v); - - clock_t t2 = std::clock(); - add(&tm, "cvec_sort", t2 - t1); - printf("#");*/ - } - - // == ITERATE - { - clock_t t1 = std::clock(); - - for (int i=0; i>1); ++i) - { - number_sink += *cvec_i_back(&v); - cvec_i_pop_back(&v); - } - clock_t t2 = std::clock(); - add(&tm, "cvec_pop_back", t2 - t1); - printf("#"); - } - cvec_i_del(&v); -} - - -using_cdeq(i, int); - -void test_cdeq(const int num_iterations) -{ - cdeq_i d = cdeq_i_with_capacity(num_iterations + 2); - stc64_t rng = stc64_init(0); - - // == PUSH_BACK - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - cdeq_i_push_back(&d, stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "cdeq_push_back", t2 - t1); - printf("#"); - } - - // == INSERT FRONT - { - clock_t t1 = std::clock(); - - for (int i=0; i<(num_iterations>>1); ++i) - { - cdeq_i_push_front(&d, stc64_rand(&rng)); - } - - clock_t t2 = std::clock(); - add(&tm, "cdeq_push_front", t2 - t1); - printf("#"); - } - - // == SORT - {/* - clock_t t1 = std::clock(); - - std::sort(d.begin(), d.end()); - - clock_t t2 = std::clock(); - add(&tm, "deque_sort", t2 - t1); - printf("#");*/ - } - - // == ITERATE - { - clock_t t1 = std::clock(); - - for (int i=0; i>1); ++i) - { - number_sink += *cdeq_i_back(&d); - cdeq_i_pop_back(&d); - } - - clock_t t2 = std::clock(); - add(&tm, "cdeq_pop_back", t2 - t1); - printf("#"); - } - cdeq_i_del(&d); -} - - - -int main(void) -{ - int num_iterations = 500000000; // 100M elements * 4 bytes. - num_iterations = (num_iterations >> 1) << 1; // Make sure num_iterations is even. - - std::cout << "Doing tests..." << std::endl; - - test_vector(num_iterations); - test_deque(num_iterations); - - test_cvec(num_iterations); - test_cdeq(num_iterations); - - std::cout << "done.\n"; - - c_foreach (i, cvec_si, tm) { - std::cout << i.ref->first << ": " << ((float) i.ref->second / CLOCKS_PER_SEC) << std::endl; - } - cvec_si_del(&tm); - return 0; -} \ No newline at end of file -- cgit v1.2.3