summaryrefslogtreecommitdiff
path: root/tests/src/performance/perflab/EnumPerf.cs
blob: 1e3a493686520170a6ffb0703656d763d529dff1 (plain)
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Xunit.Performance;
using System;
using System.Reflection;
using Xunit;

namespace PerfLabTests
{
    public enum Color
    {
        Black,
        White,
        Red,
        Brown,
        Yellow,
        Purple,
        Orange
    }

    public class EnumPerf
    {
        [Benchmark(InnerIterationCount = 300000)]
        [InlineData(Color.Red)]
        public static void EnumCompareTo(Color color)
        {
            Color white = Color.White;

            foreach (var iteration in Benchmark.Iterations)
                using (iteration.StartMeasurement())
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                        color.CompareTo(white);
        }

        [Benchmark(InnerIterationCount = 300000)]
        public static Type ObjectGetType()
        {
            Type tmp = null;
            Color black = Color.Black;

            foreach (var iteration in Benchmark.Iterations)
                using (iteration.StartMeasurement())
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                        tmp = black.GetType();

            return tmp;
        }

        [Benchmark(InnerIterationCount = 300000)]
        public static Type ObjectGetTypeNoBoxing()
        {
            Type tmp = null;
            object black = Color.Black;

            foreach (var iteration in Benchmark.Iterations)
                using (iteration.StartMeasurement())
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                        tmp = black.GetType();

            return tmp;
        }

        [Benchmark(InnerIterationCount = 300000)]
        public static bool EnumEquals()
        {
            Color black = Color.Black;
            Color white = Color.White;
            bool tmp = false;

            foreach (var iteration in Benchmark.Iterations)
                using (iteration.StartMeasurement())
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                        tmp = black.Equals(white);

            return tmp;
        }
    }
}