main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <iostream>
  2. #include <list>
  3. #include <memory>
  4. #include <ctime>
  5. #include <random>
  6. #include <cassert>
  7. #include "StackAllocator.h"
  8. #include "XorList.h"
  9. const int NUM_CONTENT = 10000000;
  10. int random_nums[NUM_CONTENT * 2];
  11. int main() {
  12. std::random_device rd;
  13. std::mt19937 mt(rd());
  14. std::uniform_int_distribution<int> random_gen(-128256512, 128256512);
  15. for (int i = 0; i < NUM_CONTENT * 2; i++) {
  16. random_nums[i] = random_gen(mt);
  17. }
  18. // for (const auto &i : random_nums)
  19. // std::cout << i << " ";
  20. // std::cout << "\n\n";
  21. {
  22. std::list<int> b;
  23. std::clock_t start_std_allocator = std::clock();
  24. for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
  25. b.push_front(random_nums[i]);
  26. if (b.front() != random_nums[i]) {
  27. std::cout << "AAAAAAAAAAAAAAAA: front is " << b.front() << ", expected: " << random_nums[i]
  28. << std::endl;
  29. }
  30. b.push_back(random_nums[i + 1]);
  31. if (b.back() != random_nums[i + 1]) {
  32. std::cout << "BBBBBBBBBBBBBBBB: back is " << b.back() << ", expected: " << random_nums[i + 1]
  33. << std::endl;
  34. }
  35. }
  36. std::clock_t end_std_allocator = std::clock();
  37. std::cout << "Time for std::list with std::allocator is "
  38. << (end_std_allocator - start_std_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms" << std::endl;
  39. //
  40. // std::cout << "=============DATA BEGIN===========\n";
  41. // for (auto k = b.begin(); k != b.end(); ++k)
  42. // std::cout << *k << " ";
  43. // std::cout << "\n==============DATA END============\n\n";
  44. }
  45. {
  46. XorList<int, StackAllocator<int>> a;
  47. std::clock_t start_stack_allocator = std::clock();
  48. for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
  49. a.push_front(random_nums[i]);
  50. if (a.front() != random_nums[i]) {
  51. std::cout << "AAAAAAAAAAAAAAAA: front is " << a.front() << ", expected: " << random_nums[i]
  52. << std::endl;
  53. }
  54. a.push_back(random_nums[i + 1]);
  55. if (a.back() != random_nums[i + 1]) {
  56. std::cout << "BBBBBBBBBBBBBBBB: back is " << a.back() << ", expected: " << random_nums[i + 1]
  57. << std::endl;
  58. }
  59. }
  60. std::clock_t end_stack_allocator = std::clock();
  61. std::cout << "Time for XorList with StackAllocator is "
  62. << (end_stack_allocator - start_stack_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms"
  63. << std::endl;
  64. //
  65. // std::cout << "=============DATA BEGIN===========\n";
  66. // for (auto k = a.begin(); k != a.end(); ++k)
  67. // std::cout << *k << " ";
  68. // std::cout << "\n==============DATA END============\n\n";
  69. }
  70. return 0;
  71. }