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