123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include <iostream>
- #include <list>
- #include <memory>
- #include <ctime>
- #include <random>
- #include <cassert>
- #include "StackAllocator.h"
- #include "XorList.h"
- const int NUM_CONTENT = 10000000;
- int random_nums[NUM_CONTENT * 2];
- int main() {
- std::random_device rd;
- std::mt19937 mt(rd());
- std::uniform_int_distribution<int> random_gen(-128256512, 128256512);
- for (int i = 0; i < NUM_CONTENT * 2; i++) {
- random_nums[i] = random_gen(mt);
- }
- // for (const auto &i : random_nums)
- // std::cout << i << " ";
- // std::cout << "\n\n";
- {
- std::list<int> b;
- std::clock_t start_std_allocator = std::clock();
- for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
- b.push_front(random_nums[i]);
- if (b.front() != random_nums[i]) {
- std::cout << "AAAAAAAAAAAAAAAA: front is " << b.front() << ", expected: " << random_nums[i]
- << std::endl;
- }
- b.push_back(random_nums[i + 1]);
- if (b.back() != random_nums[i + 1]) {
- std::cout << "BBBBBBBBBBBBBBBB: back is " << b.back() << ", expected: " << random_nums[i + 1]
- << std::endl;
- }
- }
- std::clock_t end_std_allocator = std::clock();
- std::cout << "Time for std::list with std::allocator is "
- << (end_std_allocator - start_std_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms" << std::endl;
- //
- // std::cout << "=============DATA BEGIN===========\n";
- // for (auto k = b.begin(); k != b.end(); ++k)
- // std::cout << *k << " ";
- // std::cout << "\n==============DATA END============\n\n";
- }
- {
- XorList<int, StackAllocator<int>> a;
- std::clock_t start_stack_allocator = std::clock();
- for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
- a.push_front(random_nums[i]);
- if (a.front() != random_nums[i]) {
- std::cout << "AAAAAAAAAAAAAAAA: front is " << a.front() << ", expected: " << random_nums[i]
- << std::endl;
- }
- a.push_back(random_nums[i + 1]);
- if (a.back() != random_nums[i + 1]) {
- std::cout << "BBBBBBBBBBBBBBBB: back is " << a.back() << ", expected: " << random_nums[i + 1]
- << std::endl;
- }
- }
- std::clock_t end_stack_allocator = std::clock();
- std::cout << "Time for XorList with StackAllocator is "
- << (end_stack_allocator - start_stack_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms"
- << std::endl;
- //
- // std::cout << "=============DATA BEGIN===========\n";
- // for (auto k = a.begin(); k != a.end(); ++k)
- // std::cout << *k << " ";
- // std::cout << "\n==============DATA END============\n\n";
- }
- return 0;
- }
|