aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/benchmark/ascii/main.cpp
blob: 49ad13228d84e62b349f7ad95a177e6a0503d337 (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
118
119
120
121
122
123
#include <library/cpp/testing/benchmark/bench.h>

#include <util/generic/xrange.h>
#include <util/string/ascii.h>
#include <util/generic/bitmap.h>
#include <util/generic/singleton.h>

namespace {
    struct TUpperMap: public TBitMap<256> {
        inline TUpperMap() noexcept {
            for (unsigned i = 'A'; i <= 'Z'; ++i) {
                Set((ui8)i);
            }
        }

        inline char ToLower(char x) const noexcept {
            return Get((ui8)x) ? x + ('a' - 'A') : x;
        }
    };
 
    struct TToLowerLookup { 
        char Table[256]; 
 
        TToLowerLookup() { 
            for (size_t i : xrange(256)) { 
                Table[i] = AsciiToLower(i); 
            } 
        } 
 
        char ToLower(char x) const noexcept { 
            return Table[(ui8)x]; 
        } 
    }; 
}

static inline char FastAsciiToLower(char c) {
    return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}

static inline char FastAsciiToLower2(char c) {
    return c + ('a' - 'A') * (int)(c >= 'A' && c <= 'Z');
}

Y_CPU_BENCHMARK(AsciiToLower, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(AsciiToLower(j));
        }
    }
}

Y_CPU_BENCHMARK(AsciiToLowerChar, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(AsciiToLower((char)j));
        }
    }
}

Y_CPU_BENCHMARK(FastAsciiToLower, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(FastAsciiToLower(j));
        }
    }
}

Y_CPU_BENCHMARK(FastAsciiToLower2, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(FastAsciiToLower2(j));
        }
    }
}

Y_CPU_BENCHMARK(BitMapAsciiToLower, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(Singleton<TUpperMap>()->ToLower(j));
        }
    }
}

Y_CPU_BENCHMARK(LookupAsciiToLower, iface) { 
    for (const auto i : xrange(iface.Iterations())) { 
        Y_UNUSED(i); 
 
        for (int j = 0; j < 256; ++j) { 
            Y_DO_NOT_OPTIMIZE_AWAY(Singleton<TToLowerLookup>()->ToLower(j)); 
        } 
    } 
} 
 
Y_CPU_BENCHMARK(LookupAsciiToLowerNoSingleton, iface) { 
    TToLowerLookup lookup; 
    for (const auto i : xrange(iface.Iterations())) { 
        Y_UNUSED(i); 
 
        for (int j = 0; j < 256; ++j) { 
            Y_DO_NOT_OPTIMIZE_AWAY(lookup.ToLower(j)); 
        } 
    } 
} 
 
Y_CPU_BENCHMARK(tolower, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);

        for (int j = 0; j < 256; ++j) {
            Y_DO_NOT_OPTIMIZE_AWAY(tolower(j));
        }
    }
}