aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/benchmark/cont_speed/main.cpp
blob: 072f3e3114561e98a7045d54644be98643fa5202 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <library/cpp/testing/benchmark/bench.h> 
 
#include <util/generic/xrange.h> 
#include <util/generic/string.h> 
#include <util/generic/vector.h> 
#include <util/generic/buffer.h> 
 
template <class C> 
Y_NO_INLINE void Run(const C& c) { 
    for (size_t i = 0; i < c.size(); ++i) { 
        Y_DO_NOT_OPTIMIZE_AWAY(c[i]); 
    } 
} 
 
template <class C> 
void Do(size_t len, auto& iface) { 
    C c(len, 0); 
 
    for (auto i : xrange(iface.Iterations())) { 
        Y_UNUSED(i); 
        Run(c); 
    } 
} 
 
Y_CPU_BENCHMARK(TVector10, iface) { 
    Do<TVector<char>>(10, iface); 
} 
 
Y_CPU_BENCHMARK(TVector100, iface) { 
    Do<TVector<char>>(100, iface); 
} 
 
Y_CPU_BENCHMARK(TVector1000, iface) { 
    Do<TVector<char>>(1000, iface); 
} 
 
Y_CPU_BENCHMARK(TString10, iface) { 
    Do<TString>(10, iface); 
} 
 
Y_CPU_BENCHMARK(TString100, iface) { 
    Do<TString>(100, iface); 
} 
 
Y_CPU_BENCHMARK(TString1000, iface) { 
    Do<TString>(1000, iface); 
} 
 
Y_CPU_BENCHMARK(StdString10, iface) { 
    Do<std::string>(10, iface); 
} 
 
Y_CPU_BENCHMARK(StdString100, iface) { 
    Do<std::string>(100, iface); 
} 
 
Y_CPU_BENCHMARK(StdString1000, iface) { 
    Do<std::string>(1000, iface); 
} 
 
struct TBuf: public TBuffer { 
    TBuf(size_t len, char v) { 
        for (size_t i = 0; i < len; ++i) { 
            Append(v); 
        } 
    } 
 
    inline const auto& operator[](size_t i) const noexcept { 
        return *(data() + i); 
    } 
}; 
 
Y_CPU_BENCHMARK(TBuffer10, iface) { 
    Do<TBuf>(10, iface); 
} 
 
Y_CPU_BENCHMARK(TBuffer100, iface) { 
    Do<TBuf>(100, iface); 
} 
 
Y_CPU_BENCHMARK(TBuffer1000, iface) { 
    Do<TBuf>(1000, iface); 
} 
 
struct TArr { 
    inline TArr(size_t len, char ch) 
        : A(new char[len]) 
        , L(len) 
    { 
        for (size_t i = 0; i < L; ++i) { 
            A[i] = ch; 
        } 
    } 
 
    inline const auto& operator[](size_t i) const noexcept { 
        return A[i]; 
    } 
 
    inline size_t size() const noexcept { 
        return L; 
    } 
 
    char* A; 
    size_t L; 
}; 
 
Y_CPU_BENCHMARK(Pointer10, iface) { 
    Do<TArr>(10, iface); 
} 
 
Y_CPU_BENCHMARK(Pointer100, iface) { 
    Do<TArr>(100, iface); 
} 
 
Y_CPU_BENCHMARK(Pointer1000, iface) { 
    Do<TArr>(1000, iface); 
}