aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/benchmark/singleton/main.cpp
blob: 2b06bd371d76706ce69441ab0a83982cbbdac5bd (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
#include <library/cpp/testing/benchmark/bench.h>

#include <util/generic/singleton.h>
#include <util/generic/xrange.h>

char& FF1() noexcept;
char& FF2() noexcept;

namespace {
    struct X {
        inline X() {
        }

        char Buf[100];
    };

    inline X& F1() noexcept {
        static X x;

        return x;
    }

    inline X& F2() noexcept {
        return *Singleton<X>();
    }
}

Y_CPU_BENCHMARK(MagicStatic, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);
        Y_DO_NOT_OPTIMIZE_AWAY(F1().Buf);
    }
}

Y_CPU_BENCHMARK(Singleton, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);
        Y_DO_NOT_OPTIMIZE_AWAY(F2().Buf);
    }
}

Y_CPU_BENCHMARK(MagicStaticNI, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);
        Y_DO_NOT_OPTIMIZE_AWAY(FF1());
    }
}

Y_CPU_BENCHMARK(SingletonNI, iface) {
    for (const auto i : xrange(iface.Iterations())) {
        Y_UNUSED(i);
        Y_DO_NOT_OPTIMIZE_AWAY(FF2());
    }
}