speed_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #define _HAS_ITERATOR_DEBUGGING 0
  11. #include <boost/property_tree/ptree.hpp>
  12. #include <boost/format.hpp>
  13. #include <boost/shared_array.hpp>
  14. #include <iostream>
  15. #include <ctime>
  16. #include <algorithm>
  17. using namespace std;
  18. using namespace boost;
  19. using namespace boost::property_tree;
  20. string dummy;
  21. vector<string> keys;
  22. vector<string> shuffled_keys;
  23. void prepare_keys(int size)
  24. {
  25. // Prepare keys
  26. keys.clear();
  27. for (int i = 0; i < size; ++i)
  28. keys.push_back((format("%d") % i).str());
  29. shuffled_keys = keys;
  30. srand(0);
  31. random_shuffle(shuffled_keys.begin(), shuffled_keys.end());
  32. }
  33. void clock_push_back(int size)
  34. {
  35. prepare_keys(size);
  36. int max_repeats = 1000000 / size;
  37. shared_array<ptree> pt_array(new ptree[max_repeats]);
  38. int n = 0;
  39. clock_t t1 = clock(), t2;
  40. do
  41. {
  42. if (n >= max_repeats)
  43. break;
  44. ptree &pt = pt_array[n];
  45. for (int i = 0; i < size; ++i)
  46. pt.push_back(ptree::value_type(shuffled_keys[i], ptree()));
  47. t2 = clock();
  48. ++n;
  49. } while (t2 - t1 < CLOCKS_PER_SEC);
  50. cout << " push_back (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
  51. }
  52. void clock_find(int size)
  53. {
  54. prepare_keys(size);
  55. ptree pt;
  56. for (int i = 0; i < size; ++i)
  57. pt.push_back(ptree::value_type(keys[i], ptree("data")));
  58. int n = 0;
  59. clock_t t1 = clock(), t2;
  60. do
  61. {
  62. for (int i = 0; i < size; ++i)
  63. pt.find(shuffled_keys[i]);
  64. t2 = clock();
  65. ++n;
  66. } while (t2 - t1 < CLOCKS_PER_SEC);
  67. cout << " find (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
  68. }
  69. void clock_erase(int size)
  70. {
  71. prepare_keys(size);
  72. int max_repeats = 100000 / size;
  73. shared_array<ptree> pt_array(new ptree[max_repeats]);
  74. ptree pt;
  75. for (int n = 0; n < max_repeats; ++n)
  76. for (int i = 0; i < size; ++i)
  77. pt_array[n].push_back(ptree::value_type(keys[i], ptree("data")));
  78. int n = 0;
  79. clock_t t1 = clock(), t2;
  80. do
  81. {
  82. if (n >= max_repeats)
  83. break;
  84. ptree &pt = pt_array[n];
  85. for (int i = 0; i < size; ++i)
  86. pt.erase(shuffled_keys[i]);
  87. t2 = clock();
  88. ++n;
  89. } while (t2 - t1 < CLOCKS_PER_SEC);
  90. cout << " erase (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
  91. }
  92. int main()
  93. {
  94. // push_back
  95. clock_push_back(10);
  96. clock_push_back(100);
  97. clock_push_back(1000);
  98. // erase
  99. clock_erase(10);
  100. clock_erase(100);
  101. clock_erase(1000);
  102. // find
  103. clock_find(10);
  104. clock_find(100);
  105. clock_find(1000);
  106. }