dictionary.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "Timer.h"
  4. //#define RBTREE_VERIFY
  5. #include "Dictionary.h"
  6. #include "Str.h"
  7. #include <map>
  8. using namespace std;
  9. using namespace Crown;
  10. void timeCompareWithStlMap();
  11. void dictRemove();
  12. int main()
  13. {
  14. timeCompareWithStlMap();
  15. dictRemove();
  16. getchar();
  17. }
  18. void dictRemove()
  19. {
  20. Dictionary<int, Crown::Str> dict;
  21. for(int i=0; i<20; i++)
  22. {
  23. dict[i] = "item " + Str(i*5);
  24. }
  25. dict.Remove(7);
  26. cout << "Dictionary with item 10 removed:" << endl;
  27. Dictionary<int, Crown::Str>::Enumerator e = dict.getBegin();
  28. while (e.next())
  29. {
  30. cout << "dict[" << e.current().key << "] = " << e.current().value.c_str() << endl;
  31. }
  32. dict.Clear();
  33. }
  34. void timeCompareWithStlMap()
  35. {
  36. Timer* tim = Timer::GetInstance();
  37. tim->Reset();
  38. int k = 1000000;
  39. int dictAddTime, dictRemoveTime, mapAddTime, mapRemoveTime;
  40. int dictFindTime, mapFindTime;
  41. map<int, int> stlmap;
  42. Dictionary<int, int> dict;
  43. //------Dict performance test------
  44. tim->Reset();
  45. for(int i=0; i<k; i++)
  46. {
  47. dict[i] = i;
  48. //dict.add(i, i);
  49. }
  50. dictAddTime = tim->GetMicroseconds();
  51. cout << "Elements added to Dictionary: " << dict.GetSize() << endl;
  52. tim->Reset();
  53. for(int i=0; i<k; i++)
  54. {
  55. assert(dict[i] == i);
  56. }
  57. dictFindTime = tim->GetMicroseconds();
  58. cout << "Elements searched" << endl;
  59. tim->Reset();
  60. for(int i=0; i<k; i++)
  61. {
  62. dict.Remove(i);
  63. }
  64. dictRemoveTime = tim->GetMicroseconds();
  65. cout << "Elements after removal on Dictionary: " << dict.GetSize() << endl;
  66. //------Map performance test------
  67. tim->Reset();
  68. for(int i=0; i<k; i++)
  69. {
  70. stlmap[i] = i;
  71. //stlmap.insert(map<int, int>::value_type(i, i));
  72. }
  73. mapAddTime = tim->GetMicroseconds();
  74. cout << "Elements added to StlMap: " << stlmap.size() << endl;
  75. tim->Reset();
  76. for(int i=0; i<k; i++)
  77. {
  78. assert(stlmap[i] == i);
  79. }
  80. mapFindTime = tim->GetMicroseconds();
  81. cout << "Elements searched" << endl;
  82. tim->Reset();
  83. for(int i=0; i<k; i++)
  84. {
  85. stlmap.erase(i);
  86. }
  87. mapRemoveTime = tim->GetMicroseconds();
  88. cout << "Elements after removal on StlMap: " << stlmap.size() << endl;
  89. cout << "Dictionary:" << endl;
  90. cout << "Add: " << dictAddTime << " us, Remove: " << dictRemoveTime << " us, Find: " << dictFindTime << " us" << endl;
  91. cout << "Add: " << 1.0f*dictAddTime/k << " us/add, Remove: " << 1.0*dictRemoveTime/k << " us/remove, Find: " << 1.0*dictFindTime/k << " us/find" << endl;
  92. cout << "StlMap:" << endl;
  93. cout << "Add: " << mapAddTime << " us, Remove: " << mapRemoveTime << " us, Find: " << mapFindTime << " us" << endl;
  94. cout << "Add: " << 1.0f*mapAddTime/k << " us/add, Remove: " << 1.0*mapRemoveTime/k << " us/remove, Find: " << 1.0*mapFindTime/k << " us/find" << endl;
  95. }