fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sys/timeb.h>
  5.  
  6. int GetMilliCount()
  7. {
  8. // Something like GetTickCount but portable
  9. // It rolls over every ~ 12.1 days (0x100000/24/60/60)
  10. // Use GetMilliSpan to correct for rollover
  11. timeb tb;
  12. ftime( &tb );
  13. int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
  14. return nCount;
  15. }
  16.  
  17. struct S
  18. {
  19. unsigned int a;
  20. void* b;
  21.  
  22. bool operator==(const S& other) const
  23. {
  24. return a == other.a && b == other.b;
  25. }
  26. };
  27.  
  28. template <typename Iterator>
  29. int count_eq(Iterator begin, Iterator end)
  30. {
  31. int result = 0;
  32. for (Iterator i = begin; i != end; ++i) {
  33. for (Iterator j = i + 1; j != end; ++j) {
  34. result += *i == *j;
  35. }
  36. }
  37. return result;
  38. }
  39.  
  40. template <typename Iterator>
  41. void mesure(Iterator begin, Iterator end)
  42. {
  43. long long t0 = GetMilliCount();
  44. int res = count_eq(begin, end);
  45. long long t1 = GetMilliCount();
  46. std::cout << "result: " << res <<"; Time: "<<(t1-t0)<<"\n";
  47. }
  48.  
  49. int main()
  50. {
  51. const unsigned int Size = 20000;
  52. std::vector<unsigned long long> l;
  53. for (int i = 0; i < Size; i++) {
  54. l.push_back(i% (Size/10));
  55. }
  56.  
  57. std::vector<S> s;
  58. for (int j = 0; j < Size; j++) {
  59. S el;
  60. el.a = j% (Size/10);
  61. el.b = (void*)(j% (Size/10));
  62. s.push_back(el);
  63. }
  64.  
  65. mesure(l.begin(), l.end());
  66. mesure(s.begin(), s.end());
  67. mesure(l.begin(), l.end());
  68. mesure(s.begin(), s.end());
  69. }
  70.  
Success #stdin #stdout 1.64s 2964KB
stdin
Standard input is empty
stdout
result: 90000; Time: 388
result: 90000; Time: 443
result: 90000; Time: 387
result: 90000; Time: 441